Spring-Topic

生产者

1. pom文件


    org.springframework.boot
    spring-boot-starter-activemq

2. yml文件

# springboot 启动端口
server:
  port: 8083
# activeMQ配置
spring:
  activemq:
    broker-url: tcp://112.124.20.231:61616 # activeMQ服务器ip
    user: admin
    password: admin
  # 指定连接队列还是主题
  jms:
    pub-sub-domain: true # false = Queue | true = Topic
# 自己定义队列名称
myTopicName: springboot-activemq-topic

3.配置bean

@Component
@EnableJms
public class ConfigBean {
    @Value("${myTopicName}")
    private String topicName;

    @Bean
    public ActiveMQTopic activeMQTopic(){
        return new ActiveMQTopic(topicName);
    }
}

4. Topic_Producer

@Component
public class Topic_Producer {
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
    @Autowired
    private ActiveMQTopic activeMQTopic;

    @Scheduled(fixedDelay = 3000)
    public void producer(){
        jmsMessagingTemplate.convertAndSend(activeMQTopic,"主题消息:"+ UUID.randomUUID().toString());
    }
}

5.启动类

@SpringBootApplication
@EnableScheduling
public class BootTopicProducerApplication {

    public static void main(String[] args) {
        SpringApplication.run(BootTopicProducerApplication.class, args);
    }

}

消费者

pom.xml


    org.springframework.boot
    spring-boot-starter-activemq

yml文件

# springboot 启动端口
server:
  port: 8084
# activeMQ配置
spring:
  activemq:
    broker-url: tcp://112.124.20.231:61616 # activeMQ服务器ip
    user: admin
    password: admin
  # 指定连接队列还是主题
  jms:
    pub-sub-domain: true # false = Queue | true = Topic
# 自己定义队列名称
myTopicName: springboot-activemq-topic

Topic_Consumer

@Component
public class Topic_Consumer {
    @JmsListener(destination = "${myTopicName}")
    public void consumer(TextMessage textMessage) throws Exception{
        System.out.println("订阅者收到的消息:"+textMessage.getText());
    }
}

启动

@SpringBootApplication
public class BootTopicConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(BootTopicConsumerApplication.class, args);
    }
}

持久版

配置configBean

@Component
@EnableJms
public class ConfigBean {
    @Value("${spring.activemq.broker-url}")
    private String brokerUrl;
    @Value("${spring.activemq.user}")
    private String user;
    @Value("${spring.activemq.password}")
    private String password;

    public ConnectionFactory connectionFactory(){
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        connectionFactory.setBrokerURL(brokerUrl);
        connectionFactory.setUserName(user);
        connectionFactory.setPassword(password);
        return connectionFactory;
    }

    @Bean(name = "jmsListenerContainerFactory")
    public DefaultJmsListenerContainerFactory defaultJmsListenerContainerFactory(){
        DefaultJmsListenerContainerFactory defaultJmsListenerContainerFactory = new DefaultJmsListenerContainerFactory();
        defaultJmsListenerContainerFactory.setConnectionFactory(connectionFactory());
        defaultJmsListenerContainerFactory.setSubscriptionDurable(true);
        defaultJmsListenerContainerFactory.setClientId("我是持久订阅者一号");
        return defaultJmsListenerContainerFactory;
    }

}

Topic_Consumer

@Component
public class Topic_Consumer {
    @JmsListener(destination = "${myTopicName}",containerFactory = "jmsListenerContainerFactory")
    public void consumer(TextMessage textMessage) throws Exception{
        System.out.println("订阅者收到的消息:"+textMessage.getText());
    }

}

你可能感兴趣的:(Spring-Topic)