AMQ初级使用(队列模式+主题模式)

队列模式

1.生产者代码:

package com.zzf.jms.queue;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class AppProducer {

    private static final  String url="tcp://192.168.1.6:61616";
    private static final  String queueName="queue-text";

    public static void main(String[] args) throws JMSException {

        //1.创建ConnectionFactory
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);

        //((ActiveMQConnectionFactory) connectionFactory).setUserName("admin");
        //((ActiveMQConnectionFactory) connectionFactory).setPassword("admin");

        //2.创建Connection
        Connection connection = connectionFactory.createConnection();

        //3.启动连接
        connection.start();

        //4.创建会话
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        //5.创建接受者目标
        Destination destination = session.createQueue(queueName);

        //6.创建目标
        MessageProducer producer = session.createProducer(destination);

        for (int i = 0; i <10 ; i++) {
            //7.创建消息
            TextMessage textMessage=session.createTextMessage("消息内容"+i);
            //8.发送消息
            producer.send(textMessage);
            //
            System.out.println(textMessage.getText());
        }

        //9.关闭连接
        connection.close();
    }
}

上文的url可以改成“tcp://localhost:61616”,或者通过cmd->ipconfig


2.消费者代码:

package com.zzf.jms.queue;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class AppConsumer {

    private static final  String url="tcp://192.168.1.6:61616";
    private static final  String queueName="queue-text";

    public static void main(String[] args) throws JMSException {

        //1.创建ConnectionFactory
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);

        //((ActiveMQConnectionFactory) connectionFactory).setUserName("admin");
        //((ActiveMQConnectionFactory) connectionFactory).setPassword("admin");

        //2.创建Connection
        Connection connection = connectionFactory.createConnection();

        //3.启动连接
        connection.start();

        //4.创建会话
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        //5.创建目标
        Destination destination = session.createQueue(queueName);

        //6.创建一个消费者
        MessageConsumer consumer = session.createConsumer(destination);

        //7.创建一个监听器
        consumer.setMessageListener(new MessageListener() {
            public void onMessage(Message message) {
                TextMessage textMessage = (TextMessage) message;
                try {
                    System.out.println("接受消息:"+textMessage.getText());
                } catch (JMSException e) {
                    e.printStackTrace();
                }

            }
        });

        //8.关闭连接
       // connection.close();
    }

}

输出(启动消费者模式,再启动生产者)

(队列模式如上)


主题模式

1.发布者代码:

package com.zzf.jms.topic;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class AppProducer {

    private static final  String url="tcp://192.168.1.6:61616";
    private static final  String topicName="topic-text";

    public static void main(String[] args) throws JMSException {

        //1.创建ConnectionFactory
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);

        //((ActiveMQConnectionFactory) connectionFactory).setUserName("admin");
        //((ActiveMQConnectionFactory) connectionFactory).setPassword("admin");

        //2.创建Connection
        Connection connection = connectionFactory.createConnection();

        //3.启动连接
        connection.start();

        //4.创建会话
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        //5.创建接受者目标
        Destination destination = session.createTopic(topicName);

        //6.创建目标
        MessageProducer producer = session.createProducer(destination);

        for (int i = 0; i <10 ; i++) {
            //7.创建消息
            TextMessage textMessage = session.createTextMessage("消息内容" + i);
            //8.发送消息
            producer.send(textMessage);
            //
            System.out.println(textMessage.getText());
        }

        //9.关闭连接
        connection.close();
    }
}

2.接受者代码:

package com.zzf.jms.topic;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class AppConsumer {

    private static final  String url="tcp://192.168.1.6:61616";
    private static final  String topicName="topic-text";

    public static void main(String[] args) throws JMSException {

        //1.创建ConnectionFactory
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);

        //((ActiveMQConnectionFactory) connectionFactory).setUserName("admin");
        //((ActiveMQConnectionFactory) connectionFactory).setPassword("admin");

        //2.创建Connection
        Connection connection = connectionFactory.createConnection();

        //3.启动连接
        connection.start();

        //4.创建会话
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        //5.创建目标
        Destination destination = session.createTopic(topicName);

        //6.创建一个消费者
        MessageConsumer consumer = session.createConsumer(destination);

        //7.创建一个监听器
        consumer.setMessageListener(new MessageListener() {
            public void onMessage(Message message) {
                TextMessage textMessage = (TextMessage) message;
                try {
                    System.out.println("接受消息:"+textMessage.getText());
                } catch (JMSException e) {
                    e.printStackTrace();
                }

            }
        });

        //8.关闭连接
       // connection.close();
    }

}

唯一需要改的地方是session.createTopic.

和队列模式一样,先启动接受者(启动一个以上),再启动发布者


每个接受者都接受到了发布者的发布信息(公众号)


注意:需要导入jar包:

        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>5.9.0</version>
        </dependency>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值