Пример #1
0
  public void sendMessage(final String textMessage) throws Exception {
    // Connection connection = null;
    // Session session = null;

    try {

      // Create a message
      String text =
          "DID IT WORK?! From: " + Thread.currentThread().getName() + " : " + this.hashCode();

      TextMessage message = session.createTextMessage(text);
      String timestamp = DateFormater.format(message.getJMSExpiration());

      // Tell the producer to send the message

      System.out.printf(
          "Sent message: %s : %s [%s]%n",
          message.hashCode(), Thread.currentThread().getName(), timestamp);

      // producer.setTimeToLive(DateTimeConstants.MILLIS_PER_HOUR);

      send(message);

    } catch (Exception e) {
      System.out.println("Caught: " + e);
      e.printStackTrace();
    } finally {
      if (connection != null) {
        connection.close();
        if (session != null) {
          session.close();
        }
      }
    }
  }
Пример #2
0
  public void produceMessage(int x) {
    try {
      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

      // Create the destination
      //            Destination destination = session.createQueue("Testqueue");
      Destination destination = session.createTopic("Testtopic");

      MessageProducer producer = session.createProducer(destination);
      producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

      // Create a messages
      String text =
          "Hello world "
              + x
              + "! From: "
              + Thread.currentThread().getName()
              + " : "
              + this.hashCode();
      TextMessage message = session.createTextMessage(text);

      // Tell the producer to send the message
      System.out.println(
          "Sent message: " + message.hashCode() + " : " + Thread.currentThread().getName());

      producer.send(message);
      session.close();
    } catch (Exception e) {
      System.out.println("Caught: " + e);
      e.printStackTrace();
    }
  }