pom文件添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.17.1</version>
</dependency>
配置 application.yml
server:
port: 8080 #springBoot项目访问端口
spring:
activemq:
broker-url: tcp://192.168.140.xx:30005 #你activeMQ的ip和端口号
user: admin #activeMq账号
password: admin #activeMq密码
pool:
enabled: true #连接池启动
max-connections: 10 #最大连接数
SpringBoot 的启动类添加解 @EnableJms
@SpringBootApplication
@EnableJms
public class MqconverterApplication {
public static void main(String[] args) {
SpringApplication.run(MqconverterApplication.class, args);
}
}
创建 activeMQ 配置类
mport org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.config.SimpleJmsListenerContainerFactory;
import javax.jms.ConnectionFactory;
import javax.jms.Queue;
/**
* 配置类
*/
@Configuration
public class ActiveMQConfig {
@Value("${spring.activemq.broker-url}")
private String brokerUrl;
@Value("${spring.activemq.user}")
private String userName;
@Value("${spring.activemq.password}")
private String password;
@Bean
public ConnectionFactory connectionFactory() {
return new ActiveMQConnectionFactory(userName, password, brokerUrl);
}
/**
* 在 Queue 模式中,对消息的监听需要对containerFactory进行配置
*/
@Bean("queueListener")
public JmsListenerContainerFactory<?> queueJmsListenerContainerFactory(ConnectionFactory connectionFactory) {
SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setPubSubDomain(false);
return factory;
}
/**
* 在 topic 模式中,对消息的监听需要对containerFactory进行配置
*/
@Bean("topicListener")
public JmsListenerContainerFactory<?> topicJmsListenerContainerFactory(ConnectionFactory connectionFactory) {
SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setPubSubDomain(true);
return factory;
}
}
pubSubDomain:为true 代表 topic 模式,false 代表队列模式,默认是false
生产者
@RestController
public class ActiveMQProducerController {
/**
* 队列模式
* @param msg 消息
* @return
*/
@PostMapping("/queue/{msg}")
public String sendQueue(@PathVariable("msg") String msg) {
ActiveMQQueue queue = new ActiveMQQueue("testq");
jmsMessagingTemplate.convertAndSend(queue, msg);
return "success";
}
/**
* topic 模式
* @param msg 消息
* @return
*/
@PostMapping("/topic/{msg}")
public String sendTopic(@PathVariable("msg") String msg) {
ActiveMQTopic topic = new ActiveMQTopic("test");
jmsMessagingTemplate.convertAndSend(topic, msg);
return "success";
}
}
消费者
import com.njc.mqconverter.property.KafkaTopicProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class ActiveMQConsumer {
@Autowired
KafkaTemplate kafkaTemplate;
/**
* queue模式的消费者
*/
@JmsListener(destination = "testq", containerFactory = "queueListener")
public void readActiveQueue(String message) {
String topic = KafkaTopicProperty.getSap();
kafkaTemplate.send(topic, message);
log.info("activeMQ 消费者接收:{}", message);
}
/**
* topic 模式的消费者
*/
@JmsListener(destination = "test", containerFactory = "topicListener")
public void readActivTopic1(String message) {
log.info("activeMQ topic 消费者1接收:{}", message);
}
/**
* topic 模式的消费者
*/
@JmsListener(destination = "test", containerFactory = "topicListener")
public void readActivTopic2(String message) {
log.info("activeMQ topic 消费者2接收:{}", message);
}
}
监听者的队列模式可以不配 containerFactory ,但topic的需要配置