public void onMessage(Message inMessage) {
    TextMessage msg = null;

    try {
      if (inMessage instanceof TextMessage) {
        msg = (TextMessage) inMessage;
        System.out.println("MESSAGE BEAN: Message received: " + msg.getText());
        long sleepTime = msg.getLongProperty("sleeptime");
        System.out.println("Sleeping for : " + sleepTime + " milli seconds ");
        Thread.sleep(sleepTime);
        queueConnection = queueConnectionFactory.createQueueConnection();
        queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        queueSender = queueSession.createSender(queue);
        TextMessage message = queueSession.createTextMessage();

        message.setText("REPLIED:" + msg.getText());
        message.setIntProperty("replyid", msg.getIntProperty("id"));
        System.out.println("Sending message: " + message.getText());
        queueSender.send(message);
      } else {
        System.out.println("Message of wrong type: " + inMessage.getClass().getName());
      }
    } catch (JMSException e) {
      e.printStackTrace();
    } catch (Throwable te) {
      te.printStackTrace();
    } finally {
      try {
        queueSession.close();
        queueConnection.close();
      } catch (Exception e) {
      }
    }
  } // onMessage
Ejemplo n.º 2
0
 public static Message queueReceiver(QueueConnection cnn, String queueName, long timeout)
     throws JMSException {
   QueueSession session = cnn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
   Queue queue = session.createQueue(queueName);
   QueueReceiver receiver = session.createReceiver(queue);
   Message msg = receiver.receive(timeout);
   if (msg != null) msg.acknowledge();
   receiver.close();
   session.close();
   return msg;
 }
Ejemplo n.º 3
0
 public static void drainQueue(QueueConnection cnn, String queue) throws Exception {
   QueueSession session = cnn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
   Queue q = session.createQueue(queue);
   QueueReceiver receiver = session.createReceiver(q);
   // Add a delay so that activeMQ can fetch messages from the broker
   // Thread.sleep(5000);
   Message msg = null;
   while ((msg = receiver.receive(1000)) != null) {
     System.out.println("Removing message: " + msg);
     msg.acknowledge();
   }
   receiver.close();
   session.close();
 }
  public static void main(String[] args) {
    try {
      QueueConnectionFactory queueConnectionFactory;
      QueueConnection queueConnection;
      QueueSession queueSession;
      QueueReceiver queueReceiver;
      Queue queue;
      TextMessage msg;

      // JNDI InitialContextを作成します
      InitialContext ctx = new InitialContext();
      // Connection FactoryとQueueをLook upします
      queueConnectionFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
      queue = (Queue) ctx.lookup(QUEUE);

      // コネクションを作成
      queueConnection = queueConnectionFactory.createQueueConnection();
      // セッションを作成
      queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
      // キューレシーバーを作成
      queueReceiver = queueSession.createReceiver(queue);
      // メッセージの配送をスタート
      queueConnection.start();
      // メッセージの受信
      while (true) {
        Message m = queueReceiver.receive(1);
        if (m != null) {
          if (m instanceof TextMessage) {
            msg = (TextMessage) m;
            System.out.println(msg.getText());
          } else {
            break;
          }
        }
      }
      // 接続を切断
      queueReceiver.close();
      queueSession.close();
      queueConnection.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public static void main(String[] args) throws Exception {
    ActiveMQConnectionFactory connectionFactory =
        new ActiveMQConnectionFactory("admin", "admin", "tcp://localhost:61616");
    QueueConnection queueConnection = connectionFactory.createQueueConnection();

    QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = queueSession.createQueue("test-queue");

    MessageProducer producer = queueSession.createProducer(queue);

    queueConnection.start();

    TextMessage textMessage =
        queueSession.createTextMessage("Current time: " + System.currentTimeMillis());

    producer.send(textMessage);
    log.info("Message sent at {}", System.currentTimeMillis());

    producer.close();
    queueSession.close();
    queueConnection.close();
  }
Ejemplo n.º 6
0
  public static void queueSend(
      QueueConnection cnn,
      String queueName,
      String payload,
      boolean transacted,
      int ack,
      String replyTo)
      throws JMSException {
    QueueSession session = cnn.createQueueSession(transacted, ack);
    Queue queue = session.createQueue(queueName);
    QueueSender sender = session.createSender(queue);
    TextMessage msg = session.createTextMessage();
    msg.setText(payload);
    msg.setJMSDeliveryMode(ack);
    if (replyTo != null) {
      msg.setJMSReplyTo(session.createQueue(replyTo));
    }

    sender.send(msg);
    sender.close();
    session.close();
  }
 public void closeOrderSession(@Disposes @Order QueueSession session) throws JMSException {
   session.close();
 }