Spring Boot中使用RabbitMQ
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package com.didispace.rabbit;
|
||||
|
||||
import org.springframework.amqp.core.Queue;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author 翟永超
|
||||
* @create 2016/9/25.
|
||||
* @blog http://blog.didispace.com
|
||||
*/
|
||||
@Configuration
|
||||
public class RabbitConfig {
|
||||
|
||||
@Bean
|
||||
public Queue helloQueue() {
|
||||
return new Queue("hello");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.didispace.rabbit;
|
||||
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author 翟永超
|
||||
* @create 2016/9/25.
|
||||
* @blog http://blog.didispace.com
|
||||
*/
|
||||
@Component
|
||||
@RabbitListener(queues = "hello")
|
||||
public class Receiver {
|
||||
|
||||
@RabbitHandler
|
||||
public void process(String hello) {
|
||||
System.out.println("Receiver : " + hello);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.didispace.rabbit;
|
||||
|
||||
import org.springframework.amqp.core.AmqpTemplate;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Component
|
||||
public class Sender {
|
||||
|
||||
@Autowired
|
||||
private AmqpTemplate rabbitTemplate;
|
||||
|
||||
public void send() {
|
||||
String context = "hello " + new Date();
|
||||
System.out.println("Sender : " + context);
|
||||
this.rabbitTemplate.convertAndSend("hello", context);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user