Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Connection connection; // to connect to the JMS
- Session session; // session for creating consumers
- Destination receiveDestination; reference to a queue/topic destination
- MessageConsumer consumer; // for receiving messages
- try {
- Properties props = new Properties();
- props.setProperty(Context.INITIAL_CONTEXT_FACTORY,
- "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
- props.setProperty(Context.PROVIDER_URL, "tcp://localhost:61616");
- // connect to the Destination called “myFirstChannel”
- // queue or topic: “queue.myFirstDestination” or
- “topic.myFirstDestination”
- props.put(("queue.myFirstDestination"), " myFirstDestination");
- Context jndiContext = new InitialContext(props);
- ConnectionFactory connectionFactory = (ConnectionFactory) jndiContext
- .lookup("ConnectionFactory");
- connection = connectionFactory.createConnection();
- session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- // connect to the receiver destination
- receiveDestination = (Destination) jndiContext.lookup("myFirstDestination");
- consumer = session.createConsumer(receiveDestination);
- consumer.setMessageListener(new MessageListener() {
- @Override
- public void onMessage(Message msg) {
- System.out.println("received: " + msg);
- }
- });
- connection.start(); // this is needed to start receiving messages
- } catch (NamingException | JMSException e) {
- e.printStackTrace();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement