简单使用ActiveMQ

Windows上安装ActiveMQ:

下载安装包:activemq官网(activemq.apache.org)

找到相应版本的windows压缩包,解压到安装目录

进入bin目录,进入win64目录,右键以管理员运行activemq.bat,运行成功后可以在命令行下面看到"ActiveMQ WebConsole available at http://0.0.0.0:8161/"

在浏览器中访问"127.0.0.1:8161",点击欢迎页面中的"Manage ActiveMQ broker" ,在弹出的身份验证框中输入用户名、密码(都是admin),进入管理界面,命令行窗口不要关闭。


还有另外一种启动方式,同样是在win64目录下,右键运行installService.bat,在系统服务中找到ActiveMQ,左边点击启动服务,可设置自动启动,还是访问"127.0.0.1:8161",点击

"Manage ActiveMQ broker",进入管理界面。


Linux上安装ActiveMQ:

cd进入安装目录,wget +linux平台相应版本的压缩包下载地址,解压文件"tar -zxvf  apache-activemq-5.14.2-bin.tar.gz",进入bin目录,启动服务"./activemq  start",可以"ps -ef | grep activemq"检查服务是否启动。同样在浏览器中访问"本机IP地址:8161",点击欢迎页面中的"Manage ActiveMQ broker" ,在弹出的身份验证框中输入用户名、密码(都是admin),进入管理界面,关闭命令"./activemq stop"。



队列模式的消息演示

我用的开发工具是idea,创建一个Maven工程"jmstest"

pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.csdn.jms</groupId>
    <artifactId>jms-test</artifactId>
    <version>1.0-SNAPSHOT</version>

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


消息生产者AppProducer
//向消息中间件发送消息
public class AppProducer {
    //61616是activemq开放的端口
    private static final String url="tcp://127.0.0.1:61616";
    private static final String queueName="queue-test";
    public static void main(String[] args) throws JMSException {
        //1.创建connectionfactory
        ConnectionFactory connectionFactory=new ActiveMQConnectionFactory(url);

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

        //3.建立连接
        connection.start();

        //4.创建会话
        Session session=connection.createSession(false,Session.AUTO_ACKNOWLEDGE);  //false:是否用事务中心处理,AUTO_ACKNOWLEDGE:自动应答

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

        //6.创建生产者
        MessageProducer producer=session.createProducer(destination);

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

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


消息消费者AppConsumer

public class AppConsumer {
    private static final String url="tcp://127.0.0.1:61616";
    private static final String queueName="queue-test";
    public static void main(String[] args) throws JMSException {
        //1.创建connectionfactory
        ConnectionFactory connectionFactory=new ActiveMQConnectionFactory(url);

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

        //3.建立连接
        connection.start();

        //4.创建会话
        Session session=connection.createSession(false,Session.AUTO_ACKNOWLEDGE);  //false:是否用事务中心处理,AUTO_ACKNOWLEDGE:自动应答

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

        //6.创建消息消费者
        MessageConsumer messageConsumer=session.createConsumer(destination);

        //7.创建一个监听器
        messageConsumer.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.关闭连接
        //connection.close();


    }
}

依次运行生产者、消费者程序,查看ActiveMQ管理界面



若开启两个消费者,则可以看到它们接收的消息分别是所有消息的一半,没有重复消息



主题模式的消息演示

代码只有很小的改动

    private static final String topicName="topic-test";
//5.创建一个目标
        Destination destination=session.createTopic(topicName);

测试看出,只有消费者先开启监听,生产者再生产消息,才能接收到消息,而且若开启多个消费者他们接收到的消息都是所有消息







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以回答这个问题。以下是一个简单ActiveMQ 示例: 1. 首先,你需要下载 ActiveMQ 并启动它。你可以从 ActiveMQ 官网下载最新版本的 ActiveMQ。 2. 然后,你需要创建一个 Java 项目,并将 ActiveMQ 的 JAR 文件添加到项目的类路径中。 3. 接下来,你需要编一个生产者和一个消费者。生产者将消息发送到 ActiveMQ 队列中,消费者从队列中接收消息并进行处理。 4. 下面是一个简单的生产者示例: ``` import javax.jms.*; import org.apache.activemq.ActiveMQConnectionFactory; public class Producer { public static void main(String[] args) throws JMSException { // 创建连接工厂 ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616"); // 创建连接 Connection connection = factory.createConnection(); connection.start(); // 创建会话 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // 创建队列 Destination destination = session.createQueue("myQueue"); // 创建生产者 MessageProducer producer = session.createProducer(destination); // 创建消息 TextMessage message = session.createTextMessage("Hello, world!"); // 发送消息 producer.send(message); // 关闭连接 connection.close(); } } ``` 5. 下面是一个简单的消费者示例: ``` import javax.jms.*; import org.apache.activemq.ActiveMQConnectionFactory; public class Consumer { public static void main(String[] args) throws JMSException { // 创建连接工厂 ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616"); // 创建连接 Connection connection = factory.createConnection(); connection.start(); // 创建会话 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // 创建队列 Destination destination = session.createQueue("myQueue"); // 创建消费者 MessageConsumer consumer = session.createConsumer(destination); // 接收消息 Message message = consumer.receive(); // 处理消息 if (message instanceof TextMessage) { TextMessage textMessage = (TextMessage) message; System.out.println("Received message: " + textMessage.getText()); } // 关闭连接 connection.close(); } } ``` 这是一个非常简单ActiveMQ 示例,它演示了如何使用 ActiveMQ 发送和接收消息。当然,实际应用中,你需要更加复杂的逻辑来处理消息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值