activeMq初学

1.在windows系统下使用activeMq
http://activemq.apache.org/activemq-5158-release.html 下载windows版本
解压打开找到bin/win64/activemq.bat 文件,双击运行
打开浏览器输入http://localhost:8161/admin/ 会弹出登录框输入用户名admin密码admin

项目pom文件中引入

<dependency>
		<groupId>org.apache.activemq</groupId>
		<artifactId>activemq-client</artifactId>
		<version>5.13.4</version>
</dependency>

1.1 点对点模式
创建生产者
public class testMQProducerQueue {

public static void main(String[] args) throws Exception {
    //1.创建连接工厂
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
    //2.获取连接
    Connection connection = connectionFactory.createConnection();
    //3.启动连接
    connection.start();
    //4.获取session
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    //5.创建队列对象
    Queue queue = session.createQueue("test-queue");
    //6.创建消息生产者
    MessageProducer producer = session.createProducer(queue);
    //7.创建消息
    TextMessage textMessage = session.createTextMessage("欢迎来到MQ世界");
    //8.发送消息
    producer.send(textMessage);
    //9.关闭资源
    producer.close();
    session.close();
    connection.close();
}

}

创建消费者

public class testMQConsumerQueue {

public static void main(String[] args) throws Exception {
    //1.创建连接工厂
    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
    //2.获取连接
    Connection connection = connectionFactory.createConnection();
    //3.启动连接
    connection.start();
    //4.获取session
    Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
    //5.创建队列对象
    Queue queue = session.createQueue("test-queue");
    //6.创建消息消费者
    MessageConsumer consumer = session.createConsumer(queue);
    //7.监听消息
    consumer.setMessageListener(new MessageListener() {
        @Override
        public void onMessage(Message message) {
        TextMessage textMessage = (TextMessage)message;
            try {
                System.out.println("接收到的消息"+textMessage.getText());
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    });
    //8.等待键盘输入
    System.in.read();
    //9.关闭资源
    consumer.close();
    session.close();
    connection.close();
}

}

1.2 发布/订阅模式

创建生产者

public class testMQProducerTopic {

    public static void main(String[] args) throws Exception {
        //1.创建连接工厂
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
        //2.获取连接
        Connection connection = connectionFactory.createConnection();
        //3.启动连接
        connection.start();
        //4.获取session
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //5.创建队列对象
        Topic topic = session.createTopic("test-topic");
        //6.创建消息生产者
        MessageProducer producer = session.createProducer(topic);
        //7.创建消息
        TextMessage textMessage = session.createTextMessage("欢迎来到MQ世界");
        //8.发送消息
        producer.send(textMessage);
        //9.关闭资源
        producer.close();
        session.close();
        connection.close();
    }

创建多个消费者

public class testMQConsumerTopic {

    public static void main(String[] args) throws Exception {
        //1.创建连接工厂
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
        //2.获取连接
        Connection connection = connectionFactory.createConnection();
        //3.启动连接
        connection.start();
        //4.获取session
        Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
        //5.创建队列对象
        Topic topic = session.createTopic("test-topic");
        //6.创建消息消费者
        MessageConsumer consumer = session.createConsumer(topic);
        //7.监听消息
        consumer.setMessageListener(new MessageListener() {
            @Override
            public void onMessage(Message message) {
            TextMessage textMessage = (TextMessage)message;
                try {
                    System.out.println("接收到的消息"+textMessage.getText());
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });
        //8.等待键盘输入
        System.in.read();
        //9.关闭资源
        consumer.close();
        session.close();
        connection.close();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值