深入学习java笔记-14.SpringBoot2.1之Acti

2019-05-08  本文已影响0人  笨鸡

MQ的使用场景

MQ未使用.png
MQ使用.png
pom.xml
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
            <version>2.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-pool</artifactId>
            <version>5.15.9</version>
        </dependency>
        <dependency>
            <groupId>org.messaginghub</groupId>
            <artifactId>pooled-jms</artifactId>
        </dependency>
application.yml
spring:
  jms:
    pub-sub-domain: true
  activemq:
    broker-url: tcp://localhost:61616
    user: admin
    password: admin
    in-memory: true
    pool:
      enabled: true
      max-connections: 100
SpringStartApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@ServletComponentScan
//@EnableAsync
//@EnableScheduling
@EnableJms
public class SpringStartApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringStartApplication.class, args);
    }
}
ActiveMQConfig.java
import org.apache.activemq.command.ActiveMQQueue;
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 javax.jms.ConnectionFactory;
import javax.jms.Queue;
import javax.jms.Topic;

@Configuration
public class ActiveMQConfig {
    @Bean
    public Queue queue() {
        return new ActiveMQQueue("common.queue");
    }

    @Bean
    public Topic topic() {
        return new ActiveMQTopic("news.topic");
    }

    @Bean
    public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ConnectionFactory activeMQConnectionFactory) {
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        bean.setPubSubDomain(true);
        bean.setConnectionFactory(activeMQConnectionFactory);
        return bean;
    }

}
ActiveMQConsumer.java
@Component
public class ActiveMQConsumer{

    @JmsListener(destination = "common.queue")
    public void receiveQueue(String text){
        System.out.println("commonConsumer收到的报文为:" + text);
    }

    @JmsListener(destination = "order.queue")
    public void receiveQueue(String text){
        System.out.println("orderConsumer收到的报文为:" + text);
    }

    @JmsListener(destination = "news.topic", containerFactory="jmsListenerContainerTopic")
    public void receive1(String text){
        System.out.println("topic receive1收到的报文为:" + text);
    }

    @JmsListener(destination = "news.topic", containerFactory="jmsListenerContainerTopic")
    public void receive2(String text){
        System.out.println("topic receive2收到的报文为:" + text);
    }

    @JmsListener(destination = "news.topic")
    public void receive3(String text){
        System.out.println("topic receive3收到的报文为:" + text);
    }

}
ActiveMQProducerService.java
import javax.jms.Destination;

public interface ActiveProducerService{
    public void sendMessage(Destination destination, final String message);
    public void sendMessage(final String message);
    public void publish(String msg);
}
ActiveMQProducerServiceImpl.java
import com.ctgu.springstart.service.ProducerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;

import javax.jms.Destination;
import javax.jms.Queue;
import javax.jms.Topic;

@Service
public class ProducerServiceImpl implements ProducerService {

    @Autowired
    private JmsMessagingTemplate jmsTemplate;

    @Autowired
    private Queue queue;

    @Autowired
    private Topic topic;

    @Override
    public void sendMessage(Destination destination, String message) {
        jmsTemplate.convertAndSend(destination, message);
    }

    @Override
    public void sendMessage(String message) {
        jmsTemplate.convertAndSend(queue, message);
    }

    @Override
    public void publish(String msg) {
        jmsTemplate.convertAndSend(topic, msg);
    }
}
ActiveMQController.java
import com.ctgu.springstart.domain.JsonData;
import com.ctgu.springstart.service.ProducerService;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.jms.Destination;

@RestController
@RequestMapping("/api/activemq")
public class ActiveMQController {

    @Autowired
    private ProducerService producerService;

    @GetMapping("order")
    public Object order(String msg){
        Destination destination = new ActiveMQQueue("order.queue");
        producerService.sendMessage(destination, msg);
        return JsonData.builder().msg("activeMQ order success").build();
    }

    @GetMapping("common")
    public Object common(String msg){
        producerService.sendMessage(msg);
        return JsonData.builder().msg("activeMQ common success").build();
    }

    @GetMapping("topic")
    public Object topic(String msg){
        producerService.publish(msg);
        return JsonData.builder().msg("activeMQ topic success").build();
    }

}
上一篇下一篇

猜你喜欢

热点阅读