在`application.properties`(或`application.yml`)中配置ActiveMQ的连接信息,配置文件参考
activemq:
or-start: true # 是否启用mgtt服务
broker-url: failover:(tcp://localhost:61616)?initialReconnectDelay=1000
user: admin
password: admin
topic-name: mytopic
jms:
pub-sub-domain: true
`pom.xml`中添加必要的依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
<version>2.3.5.RELEASE</version>
</dependency>
config代码
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.beans.factory.annotation.Value;
import javax.jms.ConnectionFactory;
import javax.jms.Topic;
@Configuration
public class ActiveMqConfig {
@Value("${activemq.topic-name:mytopic}")
private String defaultTopic ;
@Bean
public Topic topic() {
return new ActiveMQTopic(defaultTopic);
}
@Bean
public JmsListenerContainerFactory<?> topicListenerFactory(ConnectionFactory connectionFactory) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setPubSubDomain(true);
factory.setConnectionFactory(connectionFactory);
return factory;
}
}
发布消息demo
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import javax.jms.Topic;
@Service
public class ActiveMQPublisher {
@Autowired
private Topic topic;
@Autowired
private JmsMessagingTemplate jmsTemplate;
/**
* 使用默认bean配置的名称发送数据
*/
public void sendStringDefaultTopic(String message) {
this.jmsTemplate.convertAndSend(topic, message);
}
}
测试接口
import com.smart.framework.common.core.annotation.NoAuthInterface;
import com.smart.framework.common.core.object.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(value="/provider")
public class BootDefaultTopicProviderController {
@Autowired
private ActiveMQPublisher activeMQPublisher;
@NoAuthInterface
@PostMapping(value = "/sendMessage")
public ResponseResult<String> sendStringDefaultTopic(@RequestParam(name="message",required = true) String message) throws Exception {
activeMQPublisher.sendStringDefaultTopic(message);
return ResponseResult.success(message);
}
}
客户端代码
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
@Slf4j
public class TopicCustomer {
@JmsListener(destination = "${spring.activemg.topic-name:mytopic}", containerFactory = "topicListenerFactory")
public void receiveTopic(String message) throws JsonProcessingException {
log.info("TopicCustomer 收到消息:" + message);
}
}