1.首先我们安装一个客户端的ActiveMQ,也可以安装Linux的,这里使用的是windows安装的,具体的安装就不说了,下载后直接解压。
刚开始接触ActiveMQ,当时也看过各个大神的帖子,但是思路稍微有些乱,经过整理以后的文章,希望对大家有所帮助,话不多说看代码---
将jar包放在lib的目录下
首先我们配置生产者和消费者的xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!-- 引入公共的common.xml -->
<import resource="common.xml"/>
<!-- 配置JmsTemplate -->
<bean id="JmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"></property>
</bean>
<!-- 扫描实现类 -->
<bean class="com.Activemq.ServiceImpl.QueuemqServiceImpl"></bean>
</beans>
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!-- 启动注解 -->
<context:annotation-config />
<!-- Activemq为我们提供的一个connectionFactory -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://127.0.0.1:61616"></property>
</bean>
<!-- Spring的jms为我们提供的一个connectionFactory -->
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory" ref="targetConnectionFactory"></property>
</bean>
<!-- 配置消息的目的地-->
<bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="queue"/>
</bean>
<!-- 定义一个主题发布模式topic -->
<bean id="topicDestinaltion" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="topic"/>
</bean>
</beans>
配置QueueService及其他的实现类:
package com.Activemq.Service;
public interface QueueService {
void sendMessage(String message);
}
package com.Activemq.ServiceImpl;
import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ScheduledMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import com.Activemq.Service.QueueService;
public class QueuemqServiceImpl implements QueueService{
@Autowired
JmsTemplate jmsTemplate;
//这里可以修改为点对点(queueDestination)或者订阅模式(topicDestinaltion)
@Resource(name="queueDestination")
Destination destination;
@Override
public void sendMessage(final String message) {
System.out.println(message);
//JMS发送消息
jmsTemplate.send(destination, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
//创建一个消息
TextMessage textMessage = session.createTextMessage(message);
long time = 60 * 10000;
textMessage.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, time);
return textMessage;
}
});
System.out.println("发送消息"+message);
}
}
这就是大概的几个常用的信息,如果有需要的可以下载代码看一下(链接代码地址)
最后可以登陆(http://localhost:8161/admin/queues.jsp)查看mq的详细信息密码是(admin,admin)