ActiveMQ系列01

JMS 消息模型(具体见我的JMS消息篇博客)

  JMS消息服务应用程序结构支持两种模型:点对点模型,发布者/订阅者模型。  

  (1)点对点模型(Queue)

    一个生产者向一个特定的队列发布消息,一个消费者从这个队列中依次读取消息。

    模型特点:只有一个消费者获得消息。

  (2)发布者/订阅者模型(Topic)

    0个或多个订阅者可以接受特定主题的消息。

    模型特点:多个消费者可获得消息。

    Topic和Queue的最大区别在于Topic是以广播的形式,通知所有在线监听的客户端有新的消息,没有监听的客户端将收不到消息;而Queue则是以点对点的形式通知多个处于监听状态的客户端中的一个。

demo代码如下:

发送者,即生产者。

public class Sender {

    public static void main(String[] args) {

        Connection connection = null;
        //1、获取一个连接工厂池
        ActiveMQConnectionFactory activeMQConnectionFactory =
                new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER,
                        ActiveMQConnectionFactory.DEFAULT_PASSWORD,
                        "tcp://lqsz-l01275:61616");
        //2、从连接池获取一个连接
        try {
          connection = activeMQConnectionFactory.createConnection();
          connection.start();
            //3、获取session
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            //4、找目的获取destination   消费者也会从这个目的中获取消息 这个quene是继承destination类的一个类
            Queue userqueue = session.createQueue("user");
            //5、产生队列之后,然后创建消息,放入生产者对象中
            MessageProducer producer = session.createProducer(userqueue);
            TextMessage testmsg = session.createTextMessage("又增加一条信息");
            producer.send(testmsg);
            System.out.println("程序跑完了");
        } catch (JMSException e) {
            e.printStackTrace();
        }finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}

接收者,即消费者:

public class Receiver {


        public static void main(String[] args) {

        Connection connection = null;
        //1、获取一个连接工厂池
        ActiveMQConnectionFactory activeMQConnectionFactory =
                new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER,
                        ActiveMQConnectionFactory.DEFAULT_PASSWORD,
                        "tcp://lqsz-l01275:61616");
        //2、从连接池获取一个连接
        try {
            connection = activeMQConnectionFactory.createConnection();
            connection.start();
            //3、获取session
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            //4、找目的获取destination   消费者也会从这个目的中获取消息 这个quene是继承destination类的一个类
            Queue userqueue = session.createQueue("user");
            //5、产生队列之后,然后创建消息,放入生产者对象中
//            MessageProducer producer = session.createProducer(userqueue);
            MessageConsumer consumer = session.createConsumer(userqueue);
            while(true){

                TextMessage msg = (TextMessage)consumer.receive();
                System.out.println("消费者消耗的消息+++  "  + msg);
            }

        } catch (JMSException e) {
            e.printStackTrace();
        }finally {

        }

    }

    }

猜你喜欢

转载自blog.csdn.net/LB_Captain/article/details/114976054
今日推荐