spring boot 集成 (簡單示例)

分類:IT技術 時間:2017-03-25

參考文檔:

http://projects.spring.io/spring-boot/

https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples

1、配置

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=test
spring.rabbitmq.password=test
spring.rabbitmq.virtual-host=test

2、代碼

@SpringBootApplication // 自動配置
@RabbitListener(queues = "foo") // 監聽foo隊列
@EnableScheduling // 開啟定時任務
public class SampleAmqpSimpleApplication {
        // 類似xml 定義 send bean
	@Bean
	public Sender mySender() {
		return new Sender();
	}
        // 類似xml 定義 隊列
	@Bean
	public Queue fooQueue() {
		return new Queue("foo");
	}
        // 消費者處理
	@RabbitHandler
	public void process(@Payload String foo) {
		system.out.println(new Date() + ": " + foo);
	}
        // 程序入口
	public static void main(String[] args) throws Exception {
		SpringApplication.run(SampleAmqpSimpleApplication.class, args);
	}

}

生產者

public class Sender {

	@Autowired
	private RabbitTemplate rabbitTemplate;

	@Scheduled(fixedDelay = 1000L)
	public void send() {
		this.rabbitTemplate.convertAndSend("foo", "hello");
	}

}

 


Tags: private process spring public return

文章來源:


ads
ads

相關文章
ads

相關文章

ad