Skip to content

Commit

Permalink
(十三)集成RabbitMQ
Browse files Browse the repository at this point in the history
  • Loading branch information
1265400024 committed Nov 14, 2016
1 parent cef1c66 commit 303850c
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>

<!-- Rabbit MQ -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

<!-- swagger -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.training.rabbitmq.controller;

import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.training.core.dto.ResultDataDto;
import com.training.rabbitmq.sender.DemoRabbitMqSender;

/**
* 测试Rabbit MQ
*/
@RestController
@RequestMapping(value="/rabbitmq")
public class DemoRabbitMqController {

@Autowired
private DemoRabbitMqSender demoRabbitMqSender;

/**
* 发送测试消息队列
*/
@ApiOperation(value="发送测试消息队列", notes="addEntity")
@RequestMapping(value = "/addRabbitMq", method = RequestMethod.GET)
public @ResponseBody ResultDataDto addEntity(HttpSession httpSession) {
demoRabbitMqSender.send("jkljklkjljjkljl");
return ResultDataDto.addAddSuccess();
}

}
5 changes: 5 additions & 0 deletions src/main/java/com/training/rabbitmq/dto/DemoRabbitMqDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.training.rabbitmq.dto;

public class DemoRabbitMqDto {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.training.rabbitmq.receiver;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
* 消息队列接收类
*/
@Component
@RabbitListener(queues = "hello")
public class DemoRabbitMqReceiver {

@RabbitHandler
public void process(String content) {
System.out.println("Receiver : " + content);
}

}
18 changes: 18 additions & 0 deletions src/main/java/com/training/rabbitmq/sender/DemoRabbitMqSender.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.training.rabbitmq.sender;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DemoRabbitMqSender {

@Autowired
private AmqpTemplate rabbitTemplate;

public void send(String content) {
System.out.println("Sender : " + content);
this.rabbitTemplate.convertAndSend("hello", content);
}

}
5 changes: 5 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ spring:
smtp:
auth: true
timeout: 25000
rabbitmq:
host: 192.168.2.71
port: 5672
username: hxb
password: hxb


#指定log的配置文件,以及记录Spring Boot的log级别
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/logbak.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
        </encoder>
  </appender>

<root level="INFO">
<root level="ERROR">
<appender-ref ref="STDOUT" />
</root>

Expand Down

0 comments on commit 303850c

Please sign in to comment.