1.pom.xml添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
2.application.properties配置:
#activemq配置
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.in-memory=false
spring.activemq.pool.enabled=true
spring.activemq.pool.max-connections=5
spring.activemq.pool.maximum-active-session-per-connection=100
spring.activemq.pool.idle-timeout=30000
spring.activemq.pool.expiry-timeout=0
activemq.url=failover:(tcp://127.0.0.1:61616)
3.ActiveMQ的应答模式,消息重发机制,消息持久化配置:
import javax.jms.Queue;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.RedeliveryPolicy;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.core.JmsTemplate;
@Configuration
public class MQConfiguration {
@Bean(autowire = Autowire.BY_NAME, name = "testQueue")
public Queue queue() {
return new ActiveMQQueue("test.queue");
}
@Bean
public ActiveMQConnectionFactory activeMQConnectionFactory (@Value("${activemq.url}")String url,RedeliveryPolicy redeliveryPolicy){
ActiveMQConnectionFactory activeMQConnectionFactory =
new ActiveMQConnectionFactory(
"admin",
"admin",
url);
activeMQConnectionFactory.setRedeliveryPolicy(redeliveryPolicy);
return activeMQConnectionFactory;
}
@Bean
public RedeliveryPolicy redeliveryPolicy() {
RedeliveryPolicy redeliveryPolicy= new RedeliveryPolicy();
//是否在每次尝试重新发送失败后,增长这个等待时间
redeliveryPolicy.setUseExponentialBackOff(true);
//重发次数,默认为6次 这里设置为10次
redeliveryPolicy.setMaximumRedeliveries(10);
//重发时间间隔,默认为1秒
redeliveryPolicy.setInitialRedeliveryDelay(1);
//第一次失败后重新发送之前等待500毫秒,第二次失败再等待500 * 2毫秒,这里的2就是value
redeliveryPolicy.setBackOffMultiplier(2);
//是否避免消息碰撞
redeliveryPolicy.setUseCollisionAvoidance(false);
//设置重发最大拖延时间-1 表示没有拖延只有UseExponentialBackOff(true)为true时生效
redeliveryPolicy.setMaximumRedeliveryDelay(-1);
return redeliveryPolicy;
}
@Bean
public JmsTemplate jmsTemplate(ActiveMQConnectionFactory activeMQConnectionFactory,Queue queue){
JmsTemplate jmsTemplate=new JmsTemplate();
jmsTemplate.setDeliveryMode(2);//进行持久化配置 1表示非持久化,2表示持久化
jmsTemplate.setConnectionFactory(activeMQConnectionFactory);
jmsTemplate.setDefaultDestination(queue); //此处可不设置默认,在发送消息时也可设置队列
jmsTemplate.setSessionAcknowledgeMode(4);//1.自动确认2.客户端手动确认3.自动批量确认0.事务提交并确认4.单条消息确认
return jmsTemplate;
}
//定义一个消息监听器连接工厂,这里定义的是点对点模式的监听器连接工厂
@Bean(name = "jmsQueueListener")
public DefaultJmsListenerContainerFactory jmsQueueListenerContainerFactory(ActiveMQConnectionFactory activeMQConnectionFactory) {
DefaultJmsListenerContainerFactory factory =
new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(activeMQConnectionFactory);
//设置连接数
factory.setConcurrency("1-10");
//重连间隔时间
factory.setRecoveryInterval(1000L);
factory.setSessionAcknowledgeMode(4);//1.自动确认2.客户端手动确认3.自动批量确认0.事务提交并确认4.单条消息确认
return factory;
}
}
4.生产者:
import javax.jms.Destination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
@Service("producer")
public class Producer {
@Autowired // 也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装
private JmsMessagingTemplate jmsTemplate;
// 发送消息,destination是发送到的队列,message是待发送的消息
public void sendMessage(Destination destination, final String message){
jmsTemplate.convertAndSend(destination, message);
}
@JmsListener(destination="out.queue")//这里我配置了双向队列(简单来说两者都可以互发消息)
public void consumerMessage(String text){
System.out.println("从out.queue队列收到的回复报文为:"+text);
}
}
5.消费者:
import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;
import javax.jms.JMSException;
import javax.jms.Session;
import javax.jms.TextMessage;
/**
* @author Administrator
*
*/
@Component
public class Consumer {
/**
* @param text
* @param session
* @return
* @throws JMSException
*/
// 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
@JmsListener(destination = "test.queue", containerFactory = "jmsQueueListener")
// @SendTo将ruturn结果信息发送到out.queue(实现双向队列)
@SendTo("out.queue")
public String receiveQueue(final TextMessage text, Session session) throws JMSException {
String mesage = "success";
try {
System.out.println("Consumer收到的报文为:"+ text.getText());
text.acknowledge();// 使用手动签收模式,需要手动的调用,如果不在catch中调用session.recover()消息只会在重启服务后重发
} catch (Exception e) {
e.printStackTrace();
mesage = "fail";
session.recover();// 此不可省略 重发信息使用
}
return mesage;
}
}
6.测试
import javax.jms.Destination;
import org.apache.activemq.command.ActiveMQQueue;
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;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootJmsApplicationTests {
@Autowired
private Producer producer;
@Test
public void contextLoads() throws InterruptedException {
Destination destination = new ActiveMQQueue("test.queue");
for(int i=0; i<100; i++){
producer.sendMessage(destination, "myname is wms!!!");
}
}
}
7.测试结果:
Consumer收到的报文为:myname is wms!!!
从out.queue队列收到的回复报文为:success
Consumer收到的报文为:myname is wms!!!
从out.queue队列收到的回复报文为:success
Consumer收到的报文为:myname is wms!!!
从out.queue队列收到的回复报文为:success
Consumer收到的报文为:myname is wms!!!
从out.queue队列收到的回复报文为:success