pokazkod.pl

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;


public class ActiveMQConsumer {
    private static String url = ActiveMQConnection.DEFAULT_BROKER_URL; //1
    private static String subject = "test";

    public static void main(String[] args) {
        try {
            ConnectionFactory conncectionFactory = new ActiveMQConnectionFactory(url);
            Connection connection = conncectionFactory.createConnection();
            connection.start();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //2
            Destination destination = session.createQueue(subject);
            MessageConsumer consumer = session.createConsumer(destination);
            MyConsumer myConsumer = new MyConsumer();
            connection.setExceptionListener(myConsumer);
            consumer.setMessageListener(myConsumer);
            
        } catch (JMSException ex) {
            ex.printStackTrace();
        }
    }


    private static class MyConsumer implements MessageListener, ExceptionListener {
        public void onMessage(Message message) {
            if (message instanceof TextMessage) {
                TextMessage textMessage = (TextMessage) message;
                try {
                    System.out.println("Received message: " + textMessage.getText());
                } catch (JMSException ex) {
                    System.out.println("Error reading message: " + ex);
                }
            } else  {
                System.out.println("Received: " + message);
            }
        }
        public void onException(JMSException e) {
            System.out.println("JMS Exception occured.  Shutting down client.");
            System.exit(1);
        }
    }
}