Java examples for Message:JMS
Pass in connection string to ActiveMQConnectionFactory
import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageConsumer; import javax.jms.MessageListener; import javax.jms.Queue; import javax.jms.QueueConnection; import javax.jms.Session; import javax.jms.TextMessage; import org.apache.activemq.ActiveMQConnectionFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Main { private static Logger logger = LoggerFactory.getLogger(Main.class); public static void main(String[] args) { ActiveMQConnectionFactory connectionFactory = null; QueueConnection connection = null;// www .ja va 2 s . c o m try { connectionFactory = new ActiveMQConnectionFactory("failover:(nio://10.10.16.164:61617,tcp://10.10.16.164:31617)?randomize=false"); connection = connectionFactory.createQueueConnection(); connection.start(); final Session session = connection.createQueueSession(true, Session.AUTO_ACKNOWLEDGE); Queue destination = session.createQueue("Consumer.A.VirtualTopic.UserReg"); MessageConsumer consumer = session.createConsumer(destination); MessageListener listener = new MessageListener() { @Override public void onMessage(Message message) { TextMessage textMessage = (TextMessage) message; try { logger.info("consumer --->>>> {}", textMessage.getText()); session.commit(); } catch (JMSException e) { e.printStackTrace(); } } }; consumer.setMessageListener(listener); while (true) { } } catch (Exception e) { e.printStackTrace(); } finally { try { connection.close(); } catch (JMSException e) { e.printStackTrace(); } } } }