First, install erlang environment
rabbitmq language is based on elarng
Second, the installation environment rabbitmq
Please refer to the installation rabbitmq win10 under
Third, the plug-in installation rabbitmq_delayed_message_exchange
Plug Download http://www.rabbitmq.com/community-plugins.html (plug-in version must match the version and rabbitmq)
Archive downloaded, unpacked into the directory plugins installed below rabbitmq
执行 rabbitmq-plugins enable rabbitmq_delayed_message_exchange
Start rabbitmq
Dependent increase in sprigboot project pom file the following
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
The following configuration file in application.properties
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=test
spring.rabbitmq.password=123456
Project increased queue configuration
@Component
public class RabbitmqConfig {
@Bean
public CustomExchange delayExchange() {
Map<String, Object> args = new HashMap<>();
args.put("x-delayed-type", "direct");
return new CustomExchange("test_exchange", "x-delayed-message", true, false, args);
}
@Bean
public Queue queue() {
Queue queue = new Queue("test_queue", true);
return queue;
}
@Bean
public Binding binding() {
return BindingBuilder.bind(queue()).to(delayExchange()).with("test_queue").noargs();
}
}
Fifth, write a message listener class
@Component
public class MessageReceiver {
@RabbitListener (Queues = "test_queue")
public void the receive (String MSG) {
the SimpleDateFormat the SimpleDateFormat SDF new new = ( "the MM-dd-YYYY HH: mm: SS");
System.out.println ( "Message Received:" + sdf.format (new new a Date ()));
System.out.println ( "received message:" + MSG);
}
}
Five unit delay test message effects
@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitmqApplicationTests {
@Autowired
private MessageServiceImpl messageService;
@Test
public void send() {
messageService.sendMsg("test_queue_1","hello i am delay msg");
}