Activemq学习--java代码实现

上次已经配置好了mq的虚拟机环境,现在来用java代码实现mq的消息传输

一、新建一个空项目

1.导入activemq依赖包

根据安装的版本来选择依赖包

这里选择activemq-all-5.16.6

二、具体代码实现

1.生产者

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.util.Date;

public class JmsProduce {

    private static final String DEFAULT_BROKER_HOST = "tcp://IP:61616";

    public static void main(String[] args) throws JMSException {
        // 创建连接工厂
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(DEFAULT_BROKER_HOST);
        // 获取 connection
        final Connection connection = connectionFactory.createConnection("admin", "admin");
        // 启动
        connection.start();
        // 创建会话 session,参数第一个是事务,第二个是签收
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // 创建目的地,queue 或者 topic
        Queue queue = session.createQueue("queueName");
        // 创建消息的生产者
        MessageProducer producer = session.createProducer(queue);
        // 创建消息
        //目前用字符串来测试,之后可以更改
        TextMessage textMessage = session.createTextMessage("这是一条消息:" + new Date());
        // 发送消息给 mq
        producer.send(textMessage);
        // 关闭
        session.close();
        connection.close();
        System.out.println("消息发送成功~");
    }

}

2.消费者

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class JmsConsumer {
    public static void main(String[] args) throws JMSException {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://IP:61616");
        Connection connection = connectionFactory.createConnection("admin", "admin");
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue("queueName");
        // 创建消费者
        MessageConsumer consumer = session.createConsumer(queue);
        while (true) {
            // 同步阻塞方式使用 receive(),超时之前一直等待
            // receive() 方法不带参数会一直等待
            // receive(Long timeout) 会等待指定时间后退出等待
            TextMessage receive = (TextMessage) consumer.receive();
            if (receive != null) {
                System.out.println("接收到消息:" + receive);
            }else {
                break;
            }
        }
        consumer.close();
        session.close();
        connection.close();
    }
}

注意,此处生产者和消费者的运行顺序不会影响消息的接收和发送,无论什么时候打开消费者都能接受到生产者的消息

3.订阅主题消费者

在实际应用中,mq一般适用于按照主题订阅的方式来转发各个系统的消息

注意:使用主题订阅的方式传递数据,消费者启动后才会接收消息,若先启动生产者,消费者启动前的消息不会被接收。

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.util.Date;

public class JmsConsumerTopic {
    public static void main(String[] args) throws Exception{
        //创建工厂,建立连接
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://IP:61616");
        Connection connection = connectionFactory.createConnection("admin", "admin");
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //订阅topic和topic的名字
        Topic topic = session.createTopic("topicName");
        MessageConsumer consumer = session.createConsumer(topic);
        consumer.setMessageListener((message) -> {
            if (message != null && message instanceof TextMessage) {
                TextMessage textMessage = (TextMessage) message;
                try {
                    System.out.println(new Date()+"说:" + textMessage.getText());
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });
        System.in.read();
        consumer.close();
        session.close();
        connection.close();

    }
}

4.订阅主题生产者

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.util.Date;

public class JmsProduceTopic {
    public static void main(String[] args) throws Exception{
        //建立工厂
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://IP:61616");
        //建立连接
        Connection connection = connectionFactory.createConnection("admin", "admin");
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //topic消息传递,可修改topic名字
        Topic topic = session.createTopic("topicName");
        //消息传递给topic
        MessageProducer producer = session.createProducer(topic);
        //消息内容
        TextMessage textMessage = session.createTextMessage( "是真的闲");
        producer.send(textMessage);
        producer.close();
        session.close();
        connection.close();
        System.out.println("消息发送成功~");

    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值