AvtiveMQ
-
下载ActiveMQ
下载链接,可以根据需求进行下载
我使用的是windows版本的 运行简单便于测试 -
解压之后点击运行。(可以根据操作系统进行选择32位还是64位的)
-
在浏览器里打开ActiveMQ(http://localhost:8161),默认的 用户名,密码都是admin如图所示则表示打开成功
-
添加依赖 pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
- 在application.yml中添加属性
spring:
activemq:
broker-url: tcp://localhost:61616
user: admin
password: admin
pool:
enabled: false
- 消息的发送接收
一、点对点(PTP)模式
- 生产者 Producer
package com.imooc.activeMQ;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import javax.jms.Destination;
/**
* Created by SeuLab on 2018/10/22.
*/
@Service
public class Producer {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
public void sendMessage(String destinationName,String message){
Destination destination = new ActiveMQQueue(destinationName);
jmsMessagingTemplate.convertAndSend(destination,message);
}
}
- 消费者 Consumer
package com.imooc.activeMQ;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;
/**
* Created by SeuLab on 2018/10/22.
*/
@Service
public class Consumer {
@JmsListener(destination = "test.queue")
public void reviceMessage(String message){
System.out.println("接收到的数据:"+message);
}
}
- 测试类
package com.imooc.activeMQ;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.*;
/**
* Created by SeuLab on 2018/10/22.
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class ProducerTest {
@Autowired
private Producer producer;
@Test
public void sendMessage() throws Exception {
producer.sendMessage("test.queue","test.queue测试的数据");
}
}
二、发布/订阅(pub/sub)模式
- 发布者
package com.imooc.activeMQ;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import javax.jms.Destination;
/**
* Created by SeuLab on 2018/10/22.
*/
@Service
public class Publicsher {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
public void sendMessage(String destinationName,String message){
Destination destination =new ActiveMQTopic(destinationName);
jmsMessagingTemplate.convertAndSend(destination,message);
}
}
- 消费者
package com.imooc.activeMQ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.config.SimpleJmsListenerContainerFactory;
import org.springframework.stereotype.Service;
import javax.jms.ConnectionFactory;
import java.util.List;
/**
* Created by SeuLab on 2018/10/22.
*/
@Service
public class Subscriber {
@Autowired
private Publicsher publicsher;
@JmsListener(destination = "test.topic",containerFactory = "myJmsListenerContainerFactory")
public void subscriber(String message){
System.out.println("接收到的数据:"+message);
}
@Bean
JmsListenerContainerFactory<?> myJmsListenerContainerFactory(ConnectionFactory connectionFactory){
SimpleJmsListenerContainerFactory simpleJmsListenerContainerFactory = new SimpleJmsListenerContainerFactory();
simpleJmsListenerContainerFactory.setConnectionFactory(connectionFactory);
simpleJmsListenerContainerFactory.setPubSubDomain(true);
return simpleJmsListenerContainerFactory;
}
}
- 测试
package com.imooc.activeMQ;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.*;
/**
* Created by SeuLab on 2018/10/22.
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class PublicsherTest {
@Autowired
private Publicsher publicsher;
@Test
public void sendMessage() throws Exception {
publicsher.sendMessage("test.topic","test.topic测试数据");
}
}