Advertisement
Guest User

code

a guest
Mar 20th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.37 KB | None | 0 0
  1. Connection connection; // to connect to the JMS
  2. Session session; // session for creating consumers
  3. Destination receiveDestination; reference to a queue/topic destination
  4. MessageConsumer consumer; // for receiving messages
  5. try {
  6. Properties props = new Properties();
  7. props.setProperty(Context.INITIAL_CONTEXT_FACTORY,
  8.  "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
  9. props.setProperty(Context.PROVIDER_URL, "tcp://localhost:61616");
  10.  // connect to the Destination called “myFirstChannel”
  11.  // queue or topic: “queue.myFirstDestination” or
  12.  “topic.myFirstDestination
  13. props.put(("queue.myFirstDestination"), " myFirstDestination");
  14. Context jndiContext = new InitialContext(props);
  15. ConnectionFactory connectionFactory = (ConnectionFactory) jndiContext
  16. .lookup("ConnectionFactory");
  17. connection = connectionFactory.createConnection();
  18. session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  19. // connect to the receiver destination
  20. receiveDestination = (Destination) jndiContext.lookup("myFirstDestination");
  21. consumer = session.createConsumer(receiveDestination);
  22.  consumer.setMessageListener(new MessageListener() {
  23.  @Override
  24.  public void onMessage(Message msg) {
  25. System.out.println("received: " + msg);
  26.  }
  27.  });
  28.  connection.start(); // this is needed to start receiving messages
  29.  } catch (NamingException | JMSException e) {
  30. e.printStackTrace();
  31.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement