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
  /** @param args the command line arguments */
  public static void main(String[] args)
      throws NamingException, JMSException, InterruptedException {

    BasicConfigurator.resetConfiguration();
    BasicConfigurator.configure();

    try {
      Properties props = new Properties();
      props.setProperty(
          Context.INITIAL_CONTEXT_FACTORY,
          "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
      props.setProperty(Context.PROVIDER_URL, "tcp://miniserver.local:61616");
      Context context = new InitialContext(props);

      ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("ConnectionFactory");

      FileSystemListener myListener = new FileSystemListener();

      Destination destination = (Destination) context.lookup("dynamicTopics/eXistdb");

      LOG.info("Destination=" + destination);

      Connection connection = connectionFactory.createConnection();

      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

      MessageConsumer messageConsumer = session.createConsumer(destination);

      messageConsumer.setMessageListener(myListener);

      connection.start();

      LOG.info("Receiver is ready");

    } catch (Throwable t) {
      LOG.error(t.getMessage(), t);
    }
  }