之前在springMVC 封装了一套RabbitMQ 供公司使用.
二期的时候,提了一个需求. 不同的Queue 去push 数据怎么整
贴代码启动类
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @Description * @Author JakeWoo * @Date 2018/05/25 16:05 * @Version V1.0.0 * @Update 更新说明 */ @SpringBootApplication public class Start { public static void main(String[] args) { SpringApplication.run(Start.class, args); } }
配置类.
import org.springframework.amqp.core.Queue; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @Description 测试RabbitMQ 简单配置封装 * @Author JakeWoo * @Date 2018/05/23 16:35 * @Version V1.0.0 * @Update 更新说明 */ @Configuration public class RabbitConfigTest { public static final String QUEHE_NAME_1 = "testQueue1"; public static final String QUEHE_NAME_2 = "testQueue2"; @Bean public Queue queue() { return new Queue(QUEHE_NAME_1); } @Bean Queue queue2() { return new Queue(QUEHE_NAME_2); } }
测试类
import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.rabbit.support.CorrelationData; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.UUID; /** * @Description * @Author JakeWoo * @Date 2018/05/25 16:06 * @Version V1.0.0 * @Update 更新说明 */ @RestController @RequestMapping("/rabbit") public class RabbitConsumer { @Autowired RabbitTemplate rabbitTemplate; @GetMapping("/hello") public void send(String key, String value) { rabbitTemplate.convertAndSend(key, value); } @RabbitHandler @RabbitListener(queues = RabbitConfigTest.QUEHE_NAME_1) public void process(String message) throws Exception { System.out.println(RabbitConfigTest.QUEHE_NAME_1 + message); } @RabbitHandler @RabbitListener(queues = RabbitConfigTest.QUEHE_NAME_2) public void process2(String message) throws Exception { System.out.println(RabbitConfigTest.QUEHE_NAME_2 + message); } }
再贴一个配置
spring.application.name=springboot-rabbitmq spring.rabbitmq.addresses=localhost:5672 #spring.rabbitmq.host=localhost #spring.rabbitmq.port=5672 spring.rabbitmq.username=admin spring.rabbitmq.password=admin spring.rabbitmq.publisher-confirms=true spring.rabbitmq.virtual-host=/test
再加一个gradle 配置,
plugins { id 'java' } group 'com.jakewoo' version '1.0-SNAPSHOT' sourceCompatibility = 1.8 repositories { mavenCentral() } task "create-dirs" << { sourceSets*.java.srcDirs*.each { it.mkdirs() } sourceSets*.resources.srcDirs*.each { it.mkdirs() } } dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-amqp compile group: 'org.springframework.boot', name: 'spring-boot-starter-amqp', version: '1.5.13.RELEASE' compile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '1.5.13.RELEASE' compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.5.13.RELEASE' }
我是懵逼的.routingKey 直接传Queue name 消费者消费了就.
RabbitTemplate 在哪里创建的.懵逼的我也不知道.
有知道的大佬,看到了可以交流下.