activemq安装与使用

一直想要总结一下消息队列,正好最近有时间,就摆弄了一下.

准备工作

下载

首先去activemq官网下载应用.选择对应的版本,下载解压.
然后就可以使用了,我这次下载的windows版.

启动

使用cmd进入apache-activemq-5.14.3\bin目录下.然后运行activemq start程序就启动了.程序的控制台页面访问端口是8161.进入http://localhost:8161/admin/queues.jsp就可以看到现在有哪些队列.应用的访问端口默认是61616.

队列的操作

发送消息

package activemq;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * Created   on 2017/1/24.
 */
public class Sender
{

    private static final int SEND_NUMBER = 5;

    public static void main(String[]args)
    {
        ConnectionFactory connectionFactory;

        Connection connection = null;

        Session session;

        Destination destination;

        MessageProducer producer;

        connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
                ActiveMQConnection.DEFAULT_PASSWORD,
                "tcp://localhost:61616");

        try
        {
            connection = connectionFactory.createConnection();

            connection.start();

            session = connection.createSession(Boolean.TRUE,Session.AUTO_ACKNOWLEDGE);

            destination = session.createQueue("Send2Recv");

            producer = session.createProducer(destination);

            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

            TextMessage message = session.createTextMessage("this is message");


            producer.send(message);
            System.out.println(message.getText());

            session.commit();

        } catch (JMSException e)
        {
            e.printStackTrace();
        }finally
        {
            try
            {
                if(null!= connection)
                    connection.close();
            } catch (JMSException e)
            {
                e.printStackTrace();
            }



        }
    }



}

接收消息

package activemq;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;


import javax.jms.*;

/**
 * Created   on 2017/1/24.
 */
public class Receiver
{

    public static void main(String args[])
    {
        ConnectionFactory connectionFactory;

        Connection connection = null;

        Session session;

        Destination destination;

        MessageConsumer consumer;

        connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
                ActiveMQConnection.DEFAULT_PASSWORD,"tcp://localhost:61616");

        try
        {
            connection = connectionFactory.createConnection();

            connection.start();

            session = connection.createSession(Boolean.FALSE,
                    Session.AUTO_ACKNOWLEDGE);

            destination = session.createQueue("Send2Recv");

            consumer = session.createConsumer(destination);

            while (true)
            {
                TextMessage message = (TextMessage) consumer.receive();
                if(null!=message)
                {
                    System.out.println("收到消息"+message.getText());
                }
                else
                {
                    break;
                }
            }

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

            try
            {
                if(null!=connection)
                    connection.close();
            } catch (JMSException e)
            {
                e.printStackTrace();
            }
        }
    }
}

与spring项目整合

在官方的教程里,直接让引入一个activemq-all的jar包就可以了.引入过后程序一直起不来.说是slf2j绑定了两个,因为版本不一样冲突之类的.原来在activemq-all的包的pom文件里,他把他需要用到的很多jar都一起include进去了.用exclusion还没用,最后呢,改用下面依赖,解决.

   <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jms</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-spring</artifactId>
      <version>5.10.0</version>
    </dependency>

然后就是spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jms="http://www.springframework.org/schema/jms"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd">
    <!--让jms的注解生效,这个一定要注意-->
    <jms:annotation-driven/>

    <bean id="jmsFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
       <constructor-arg index="0" value="tcp://localhost:61616"/>
    </bean>

    <!-- Pooled Spring connection factory -->
    <bean id="connectionFactory"
          class="org.springframework.jms.connection.CachingConnectionFactory">
        <constructor-arg ref="jmsFactory" />
    </bean>


    <bean id="defaultDestination" class="org.apache.activemq.command.ActiveMQQueue">
         <constructor-arg index="0" value="Send2Recv"></constructor-arg>
    </bean>

    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="jmsFactory"></property>
        <property name="defaultDestination" ref="defaultDestination"></property>
    </bean>

    <bean id="jmsListenerContainerFactory" class="org.springframework.jms.config.DefaultJmsListenerContainerFactory">
        <property name="connectionFactory" ref="connectionFactory" />
        <!--同时能处理的消费数范围,只写一个数就表示最大值 -->
        <property name="concurrency" value="3-10" />
    </bean>



</beans>

发送到队列

package com.xxx.ws.service;

import com.xxx.ws.entity.Hi;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import java.io.Serializable;

/**
 * Created   on 2017/1/24.
 */
@Service
public class JmsMessageSender
{

    @Resource
    private JmsTemplate jmsTemplate;

    /**
     * send text to default destination
     * @param text
     */
    public void send(final String text)
    {
        this.jmsTemplate.send(new MessageCreator()
        {
            @Override
            public Message createMessage(Session session) throws JMSException
            {
                Message message = session.createTextMessage(text);
                message.setJMSReplyTo(new ActiveMQQueue("Recv2Send"));
                return message;
            }
        });
    }

    /**
     * Simplify the send by using convertAndSend
     * @param text
     */
    public void sendText(final String text)
    {
        this.jmsTemplate.convertAndSend(text);
    }

    /**
     * Send text message to a specified destination
     * @param dest
     * @param text
     */
    public void send(final Destination dest,final String text)
    {
        this.jmsTemplate.send(dest, new MessageCreator()
        {
            @Override
            public Message createMessage(Session session) throws JMSException
            {
                Message message = session.createTextMessage(text);
                return message;
            }
        });
    }

    public void send(final Object hi)
    {
        jmsTemplate.send(new MessageCreator()
        {
            @Override
            public Message createMessage(Session session) throws JMSException
            {
                return session.createObjectMessage((Serializable) hi);
            }
        });
    }


}

消费队列

package com.xxx.ws.service;



import org.apache.activemq.command.ActiveMQObjectMessage;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;

/**
 * Created   on 2017/1/24.
 */
@Service
public class JmsMessageReceiver
{

    private final Logger logger = LogManager.getLogger(this.getClass());

    @Resource
    private JmsTemplate jmsTemplate;



    @JmsListener(destination = "Send2Recv")
//    @SendTo("Recv2Send")
    public void onMessage(String text) throws JMSException
    {
        System.out.println("Received: " + text );
    }
}

这样就能在spring的项目里生产和消费消息了.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
安装和配置ActiveMQ,请按照以下步骤进行操作: 1. 下载安装包:访问ActiveMQ官方网站(http://activemq.apache.org/components/classic/download/),根据您的操作系统选择合适的版本进行下载。 2. 解压缩安装包:将下载的安装包解压到您选择的目录。 3. 启动ActiveMQ:进入解压后的目录中的bin/win64目录,找到activemq.bat文件,双击运行或在命令行中执行此文件。 4. 配置ActiveMQ:启动成功后,您可以使用命令行窗口打开活动MQ管理控制台。在浏览器中输入http://localhost:8161,进入管理页面。 5. 配置队列和主题:在管理页面中,您可以创建和配置队列和主题以满足您的需求。通过添加新的队列和主题来实现消息传递和发布/订阅模式。 6. 测试与使用:您现在可以使用ActiveMQ来发送和接收消息。您可以使用ActiveMQ的Java API或其他客户端库来编写代码并与ActiveMQ进行通信。 请注意,这只是一个简单的概述,您可能需要参考ActiveMQ的官方文档或其他资源以获取更详细的安装和配置说明。希望这些步骤能帮助您成功安装和配置ActiveMQ。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [ActiveMQ安装使用](https://blog.csdn.net/qq_29651203/article/details/108487924)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [ActiveMQ安装使用与配置说明](https://blog.csdn.net/zgphacker2010/article/details/127140614)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值