Spring Boot RabbitMQ 集成

1、添加Maven依赖 (或jar包)

		
		
			org.springframework.boot
			spring-boot-starter-amqp
		

2、配置数据源相关信息

#RabbitMQ
spring.application.name=spirng-boot-rabbitmq
spring.rabbitmq.host=192.168.10.15
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin

3、接受者

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

import com.cl.model.User;

@Component
@RabbitListener(queues = "userQueue")
public class HelloReceiver {

	// 接受者
	@RabbitHandler
	public void process(User user) {
		System.out.println("Receiver object : " + user);
	}

}

4、发送者

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

import com.cl.model.User;

@Component
public class HelloSender {
	@Autowired
	private AmqpTemplate rabbitTemplate;

	// 发送者
	public void send(User user) {
		System.out.println("Sender object: " + user.toString());
		this.rabbitTemplate.convertAndSend("userQueue", user);
	}
}

5、Bean对象

import java.io.Serializable;

public class User implements Serializable {
	private static final long serialVersionUID = -347894033089232321L;
	private Integer id;
	private String username;
	private String password;
	private String email;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password=" + password + ", email=" + email + "]";
	}

}

6、测试

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class) // 指定spring-boot的启动类
@WebAppConfiguration
public class TestRabbiMq {
	@Autowired
	private HelloSender helloSender;

	@Test
	public void oneToMany() throws Exception {
		User user = new User();
		user.setEmail("[email protected]");
		user.setPassword("123456");
		helloSender.send(user);
	}
}

7、异常处理

org.springframework.amqp.rabbit.listener.BlockingQueueConsumer$DeclarationException: Failed to declare queue(s):[userQueue]
	at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.attemptPassiveDeclarations(BlockingQueueConsumer.java:587) ~[spring-rabbit-1.6.1.RELEASE.jar:na]
	at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.start(BlockingQueueConsumer.java:486) ~[spring-rabbit-1.6.1.RELEASE.jar:na]
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1280) [spring-rabbit-1.6.1.RELEASE.jar:na]
	at java.lang.Thread.run(Thread.java:745) [na:1.8.0_121]
Caused by: java.io.IOException: null
	at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:106) ~[amqp-client-3.6.3.jar:na]
	at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:102) ~[amqp-client-3.6.3.jar:na]
	at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:124) ~[amqp-client-3.6.3.jar:na]
	at com.rabbitmq.client.impl.ChannelN.queueDeclarePassive(ChannelN.java:885) ~[amqp-client-3.6.3.jar:na]
	at com.rabbitmq.client.impl.ChannelN.queueDeclarePassive(ChannelN.java:61) ~[amqp-client-3.6.3.jar:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
	at org.springframework.amqp.rabbit.connection.CachingConnectionFactory$CachedChannelInvocationHandler.invoke(CachingConnectionFactory.java:916) ~[spring-rabbit-1.6.1.RELEASE.jar:na]
	at com.sun.proxy.$Proxy89.queueDeclarePassive(Unknown Source) ~[na:na]
	at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.attemptPassiveDeclarations(BlockingQueueConsumer.java:566) ~[spring-rabbit-1.6.1.RELEASE.jar:na]
	... 3 common frames omitted
Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method(reply-code=404, reply-text=NOT_FOUND - no queue 'userQueue' in vhost '/', class-id=50, method-id=10)
	at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:67) ~[amqp-client-3.6.3.jar:na]
	at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:33) ~[amqp-client-3.6.3.jar:na]
	at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:361) ~[amqp-client-3.6.3.jar:na]
	at com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:226) ~[amqp-client-3.6.3.jar:na]
	at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:118) ~[amqp-client-3.6.3.jar:na]
	... 12 common frames omitted

说明RabbitMQ中没有找到该队列,添加一个就OK

Spring Boot RabbitMQ 集成_第1张图片


你可能感兴趣的:(Spring,Boot)