依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.26</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
配置文件
spring.rabbitmq.addresses=localhost:5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.virtual-host=/
spring.rabbitmq.connection-timeout=15000
server.servlet.context-path=/
server.port=8001
spring.http.encoding.charset=UTF-8
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
spring.jackson.default-property-inclusion=non_null
消息类
package com.example.rabbitmq.springbootproducer.entity;
import java.io.Serializable;
public class Order implements Serializable{
private String id;
private String name;
private String messageID;
public Order() {
}
public Order(String id, String name, String messageID) {
this.id = id;
this.name = name;
this.messageID = messageID;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMessageID() {
return messageID;
}
public void setMessageID(String messageID) {
this.messageID = messageID;
}
}
发送消息
package com.example.rabbitmq.springbootproducer.producer;
import com.example.rabbitmq.springbootproducer.entity.Order;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class OrderSender {
@Autowired
private RabbitTemplate rabbitTemplate;
public void send(Order order) {
CorrelationData correlationData = new CorrelationData();
correlationData.setId(order.getMessageID());
rabbitTemplate.convertAndSend(
"order-exchange",
"order.abcd",
order,
correlationData); // 消息唯一ID
}
}
测试发送
package com.example.rabbitmq.springbootproducer;
import com.example.rabbitmq.springbootproducer.entity.Order;
import com.example.rabbitmq.springbootproducer.producer.OrderSender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.UUID;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootProducerApplicationTests {
@Autowired
private OrderSender orderSender;
@Test
public void contextLoads() {
}
@Test
public void testSend1() throws Exception{
Order order = new Order();
order.setId("20190409000000001");
order.setName("测试订单1");
order.setMessageID(System.currentTimeMillis() + "$" + UUID.randomUUID().toString());
orderSender.send(order);
}
}