예제 #1
2
 /**
  * Creates all the necessary objects for receiving messages from a JMS queue.
  *
  * @param ctx JNDI initial context
  * @param queueName name of queue
  * @exception NamingException if operation cannot be performed
  * @exception JMSException if JMS fails to initialize due to internal error
  */
 public void init(Context ctx, String queueName) throws NamingException, JMSException {
   qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
   qcon = qconFactory.createQueueConnection();
   qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
   queue = (Queue) ctx.lookup(queueName);
   qreceiver = qsession.createReceiver(queue);
   qreceiver.setMessageListener(this);
   qcon.start();
 }
  /** use auto acknowledge which automatically has the client say OK */
  public void doReceiveAuto() {

    if (doSetup()) {
      try {
        queueConnection = queueConnectionFactory.createQueueConnection();
        QueueSession queueSession =
            queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        QueueReceiver queueReceiver = queueSession.createReceiver(queue);
        queueConnection.start();
        while (true) {
          Message m = queueReceiver.receive(1);
          if (m != null) {
            if (m instanceof TextMessage) {
              TextMessage message = (TextMessage) m;
              log.debug("Reading message:==> " + message.getText());
            } else {
              break;
            }
          }
        }
      } catch (JMSException e) {
        log.error("Listen Exception occurred: " + e.toString());
      } finally {
        doCleanup();
      }
    }
  }
예제 #3
0
  public QLender(String queuecf, String requestQueue) {
    try {
      // Connect to the provider and get the JMS connection
      Context ctx = new InitialContext();
      QueueConnectionFactory qFactory = (QueueConnectionFactory) ctx.lookup(queuecf);
      qConnect = qFactory.createQueueConnection();

      // Create the JMS Session
      qSession = qConnect.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

      // Lookup the request queue
      requestQ = (Queue) ctx.lookup(requestQueue);

      // Now that setup is complete, start the Connection
      qConnect.start();

      // Create the message listener
      QueueReceiver qReceiver = qSession.createReceiver(requestQ);
      qReceiver.setMessageListener(this);

      System.out.println("Waiting for loan requests...");

    } catch (JMSException jmse) {
      jmse.printStackTrace();
      System.exit(1);
    } catch (NamingException jne) {
      jne.printStackTrace();
      System.exit(1);
    }
  }
예제 #4
0
파일: RouteTest.java 프로젝트: grkvlt/jms
 @Test
 public void noMatchingRoutes() throws JMSException {
   String expected = "'no matching route'";
   QueueReceiver qr = messageManager.createQueueReceiver("queue/DLQ");
   clear(qr);
   plainEvent.fire(expected);
   Message m = qr.receive(3000);
   qr.close();
   Assert.assertNull("Unexpectedly received a message", m);
 }
예제 #5
0
  @Test
  public void receiver() throws Exception {
    QueueConnection connection = null;
    QueueSession session = null;
    try {
      // 创建链接工厂
      QueueConnectionFactory factory =
          new ActiveMQConnectionFactory(
              ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, BROKER_URL);
      // 通过工厂创建一个连接
      connection = factory.createQueueConnection();
      // 启动连接
      connection.start();
      // 创建一个session会话
      session = connection.createQueueSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
      // 创建一个消息队列
      javax.jms.Queue queue = session.createQueue(TARGET);
      // 创建消息制作者
      javax.jms.QueueReceiver receiver = session.createReceiver(queue);

      receiver.setMessageListener(
          new MessageListener() {
            public void onMessage(Message msg) {
              if (msg != null) {
                // MapMessage map = (MapMessage) msg;
                try {
                  System.out.println(msg.getStringProperty("text"));

                  // System.out.println(map.getLong("time") + "接收#" +
                  // map.getString("text"));
                } catch (JMSException e) {
                  e.printStackTrace();
                }
              }
            }
          });
      // 休眠100ms再关闭
      Thread.sleep(1000 * 100);

      // 提交会话
      session.commit();

    } catch (Exception e) {
      throw e;
    } finally {
      // 关闭释放资源
      if (session != null) {
        session.close();
      }
      if (connection != null) {
        connection.close();
      }
    }
  }
예제 #6
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;
 }
예제 #7
0
파일: RouteTest.java 프로젝트: grkvlt/jms
 @Test
 public void forwardSimpleEvent_via_single_route_config() throws JMSException {
   String expected = "'configured via Route'";
   QueueReceiver qr = messageManager.createQueueReceiver("queue/DLQ");
   clear(qr);
   event_viaSingleRouteConfig.fire(expected);
   Message m = qr.receive(3000);
   qr.close();
   Assert.assertTrue(m != null);
   Assert.assertTrue(m instanceof TextMessage);
   Assert.assertEquals(expected, ((TextMessage) m).getText());
 }
예제 #8
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();
 }
예제 #9
0
  /**
   * Wait for 'count' messages on controlQueue before continuing. Called by a publisher to make sure
   * that subscribers have started before it begins publishing messages.
   *
   * <p>If controlQueue doesn't exist, the method throws an exception.
   *
   * @param prefix prefix (publisher or subscriber) to be displayed
   * @param controlQueueName name of control queue
   * @param count number of messages to receive
   */
  public static void receiveSynchronizeMessages(String prefix, String controlQueueName, int count)
      throws Exception {
    QueueConnectionFactory queueConnectionFactory = null;
    QueueConnection queueConnection = null;
    QueueSession queueSession = null;
    Queue controlQueue = null;
    QueueReceiver queueReceiver = null;

    try {
      queueConnectionFactory = SampleUtilities.getQueueConnectionFactory();
      queueConnection = queueConnectionFactory.createQueueConnection();
      queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
      controlQueue = getQueue(controlQueueName, queueSession);
      queueConnection.start();
    } catch (Exception e) {
      System.out.println("Connection problem: " + e.toString());
      if (queueConnection != null) {
        try {
          queueConnection.close();
        } catch (JMSException ee) {
        }
      }
      throw e;
    }

    try {
      System.out.println(
          prefix
              + "Receiving synchronize messages from "
              + controlQueueName
              + "; count = "
              + count);
      queueReceiver = queueSession.createReceiver(controlQueue);
      while (count > 0) {
        queueReceiver.receive();
        count--;
        System.out.println(prefix + "Received synchronize message; expect " + count + " more");
      }
    } catch (JMSException e) {
      System.out.println("Exception occurred: " + e.toString());
      throw e;
    } finally {
      if (queueConnection != null) {
        try {
          queueConnection.close();
        } catch (JMSException e) {
        }
      }
    }
  }
  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();
    }
  }
  /** manually say OK I'm ready to take the message on the client */
  public void doReceiveAck() {

    if (doSetup()) {
      try {
        queueConnection = queueConnectionFactory.createQueueConnection();
        QueueSession queueSession =
            queueConnection.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
        QueueReceiver queueReceiver = queueSession.createReceiver(queue);
        queueConnection.start();
        String info = null;
        while (true) {
          Message m = queueReceiver.receive(1);
          if (m != null) {
            if (m instanceof TextMessage) {
              TextMessage message = (TextMessage) m;
              info = message.getText();

              if (info.indexOf("12") > -1) {
                log.debug("Skipping message:==> " + message.getText());
              } else {
                log.debug("Reading message:==> " + message.getText());
                message.acknowledge();
              }

            } else {
              break;
            }
          }
        }
      } catch (JMSException e) {
        log.error("Listen Exception occurred: " + e.toString());
      } finally {
        doCleanup();
      }
    }
  }
예제 #12
0
 /**
  * Closes JMS objects.
  *
  * @exception JMSException if JMS fails to close objects due to internal error
  */
 public void close() throws JMSException {
   qreceiver.close();
   qsession.close();
   qcon.close();
 }
예제 #13
0
 private void close(QueueReceiver receiver) throws Exception {
   receiver.close();
 }
예제 #14
0
파일: RouteTest.java 프로젝트: grkvlt/jms
 private void clear(QueueReceiver qr) throws JMSException {
   while (qr.receiveNoWait() != null) ;
 }
 @Test(timeout = 30000, expected = javax.jms.IllegalStateException.class)
 public void testReceiverGetQueueFails() throws Exception {
   receiver.getQueue();
 }
 @Test(timeout = 30000)
 public void testReceiverCloseAgain() throws Exception {
   // Close it again (closing the session should have closed it already).
   receiver.close();
 }