我刚开始使用基于云的应用程序并尝试使用RabbitTemplate和RabbitMQ。
我可以使用。
将数据存储到队列中rabbitTemplate.convertAndSend(QUEUE_NAME, msg);
但是当我使用
从同一队列接收数据时rabbitTemplate.receiveAndConvert(QUEUE_NAME)
我得到例外:
ERR引起:java.io.IOException 2016-09-13T11:15:21.38 + 0530 [App / 0] ERR at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:106) 2016-09-13T11:15:21.38 + 0530 [App / 0] ERR at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:102) 2016-09-13T11:15:21.38 + 0530 [App / 0] ERR at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:124) 2016-09-13T11:15:21.38 + 0530 [App / 0] ERR at com.rabbitmq.client.impl.ChannelN.basicGet(ChannelN.java:985) 2016-09-13T11:15:21.38 + 0530 [App / 0] sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)的ERR 2016-09-13T11:15:21.38 + 0530 [App / 0] sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)的ERR 2016-09-13T11:15:21.38 + 0530 [App / 0]在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)的ERR 2016-09-13T11:15:21.38 + 0530 [App / 0] java.lang.reflect.Method.invoke的ERR(Method.java:498) 2016-09-13T11:15:21.38 + 0530 [App / 0] ERR at org.springframework.amqp.rabbit.connection.CachingConnectionFactory $ CachedChannelInvocationHandler.invoke(CachingConnectionFactory.java:625) 2016-09-13T11:15:21.38 + 0530 [App / 0] com.sun.proxy的ERR。$ Proxy55.basicGet(未知来源) 2016-09-13T11:15:21.38 + 0530 [App / 0] ERR at org.springframework.amqp.rabbit.core.RabbitTemplate $ 4.doInRabbit(RabbitTemplate.java:650) 2016-09-13T11:15:21.38 + 0530 [App / 0] ERR at org.springframework.amqp.rabbit.core.RabbitTemplate $ 4.doInRabbit(RabbitTemplate.java:646) 2016-09-13T11:15:21.38 + 0530 [App / 0] ERR at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:1045) 2016-09-13T11:15:21.38 + 0530 [App / 0] ERR ...... 50多个 2016-09-13T11:15:21.38 + 0530 [App / 0] ERR引起: com.rabbitmq.client.ShutdownSignalException:通道错误;协议方法:#method(reply-code = 404,reply-text = NOT_FOUND - no queue' testqueue' in vhost' 9cc1b4db-636e-4251-bb68-c7ed7f3be1d3',class-id = 60,method-id = 70) 2016-09-13T11:15:21.38 + 0530 [App / 0] ERR at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:67) 2016-09-13T11:15:21.38 + 0530 [App / 0] ERR at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:33) 2016-09-13T11:15:21.38 + 0530 [App / 0] ERR at com.rabbitmq.client.impl.AMQChannel $ BlockingRpcContinuation.getReply(AMQChannel.java:343) 2016-09-13T11:15:21.38 + 0530 [App / 0] ERR at com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:216) 2016-09-13T11:15:21.38 + 0530 [App / 0] ERR at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:118) 2016-09-13T11:15:21.38 + 0530 [App / 0] ERR ... 60多 2016-09-13T11:15:21.38 + 0530 [App / 0] ERR引起:com.rabbitmq.client.ShutdownSignalException:通道错误;协议方法:#method(reply-code = 404,reply-text = NOT_FOUND - no queue' testqueue' in vhost' 9cc1b4db-636e-4251-bb68-c7ed7f3be1d3',class-id = 60,method-id = 70) 2016-09-13T11:15:21.38 + 0530 [App / 0] ERR at com.rabbitmq.client.impl.ChannelN.asyncShutdown(ChannelN.java:478) 2016-09-13T11:15:21.38 + 0530 [App / 0] ERR at com.rabbitmq.client.impl.ChannelN.processAsync(ChannelN.java:315) 2016-09-13T11:15:21.38 + 0530 [App / 0] ERR at com.rabbitmq.client.impl.AMQChannel.handleCompleteInboundCommand(AMQChannel.java:144) 2016-09-13T11:15:21.38 + 0530 [App / 0] ERR at com.rabbitmq.client.impl.AMQChannel.handleFrame(AMQChannel.java:91) 2016-09-13T11:15:21.38 + 0530 [App / 0] ERR at com.rabbitmq.client.impl.AMQConnection $ MainLoop.run(AMQConnection.java:552)
我的代码:
@Configuration
@Profile("cloud")
public class RabbitConfig extends AbstractCloudConfig {
@Bean
public RabbitTemplate rabbitTemplate(){
CachingConnectionFactory cachingConnectionFactory = (CachingConnectionFactory)connectionFactory().rabbitConnectionFactory();
cachingConnectionFactory.setCacheMode(CachingConnectionFactory.CacheMode.CHANNEL);
RabbitTemplate rabbitTemplate = new RabbitTemplate(cachingConnectionFactory);
return rabbitTemplate;
}
}
我的控制器:
@RestController
@RequestMapping("mq")
public class MainController {
@Autowired
private RabbitTemplate rabbitTemplate;
private static final String QUEUE_NAME = "testqueue";
@RequestMapping(value = "/putinq/{msg}",method = RequestMethod.PUT)
public String storeMessage(@PathVariable("msg") String msg){
String result = "";
rabbitTemplate.setQueue(QUEUE_NAME);
try {
rabbitTemplate.convertAndSend(QUEUE_NAME, msg); // no exception
Thread.sleep(3000);
Object object = rabbitTemplate.receiveAndConvert(QUEUE_NAME); // getting exception here
System.out.println("Received: "+object);
result = "success";
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
}
UPDATED RabbitConfig.java
@Configuration
@Profile("cloud")
public class RabbitConfig extends AbstractCloudConfig {
private static final String QUEUE_NAME = "testqueue";
@Bean
public RabbitTemplate rabbitTemplate(){
CachingConnectionFactory cachingConnectionFactory = (CachingConnectionFactory)(connectionFactory().rabbitConnectionFactory());
System.out.println("------------------------ Rabbit mq template: " + cachingConnectionFactory.getCacheMode());
System.out.println("------------------------ cachingConnectionFactory.toString(): " + cachingConnectionFactory.toString());
cachingConnectionFactory.setCacheMode(CachingConnectionFactory.CacheMode.CHANNEL);
cachingConnectionFactory.setChannelCacheSize(25);
System.out.println("----------after set-------------- Rabbit mq template: " + cachingConnectionFactory.getCacheMode());
System.out.println("------------------------ cachingConnectionFactory.toString(): " + cachingConnectionFactory.getChannelCacheSize());
RabbitTemplate rabbitTemplate = new RabbitTemplate(cachingConnectionFactory);
System.out.println("------------------------ Rabbit mq template: " + rabbitTemplate);
return rabbitTemplate;
}
@Bean
public Queue myQueue() {
System.out.println("--------------@@---------- creating queue: ");
final boolean isDurable = true;
final boolean isExclusive = false;
final boolean autoDelete = false;
return new Queue(QUEUE_NAME, isDurable, isExclusive, autoDelete);
}
}
我尝试在互联网上搜索这个但仍然无法解决问题,长期以来一直在努力解决这个问题,感谢任何帮助。
得到了解决方案!
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
cachingConnectionFactory.setUsername("");
cachingConnectionFactory.setPassword("");
cachingConnectionFactory.setVirtualHost("");
cachingConnectionFactory.setHost("");
cachingConnectionFactory.setPort(1);
cachingConnectionFactory.setRequestedHeartBeat(30);
cachingConnectionFactory.setConnectionTimeout(30000);
RabbitAdmin admin = new RabbitAdmin(cachingConnectionFactory());
Queue queue = new Queue(QUEUE_NAME);
admin.declareQueue(queue);
TopicExchange exchange = new TopicExchange(EXCHANGE_NAME);
admin.declareExchange(exchange); admin.declareBinding(BindingBuilder.bind(queue).to(exchange).with(QUEUE_NAME));
RabbitTemplate template = new RabbitTemplate(cachingConnectionFactory());
答案 0 :(得分:3)
您需要RabbitAdmin
@Bean
来声明队列/绑定 - 它会自动找到这些类型的bean,并在建立连接时在代理上声明它们。
如果您使用Spring Boot,它会自动为您注册模板和管理员。
答案 1 :(得分:1)
您需要在配置中定义队列本身。
@Bean
public org.springframework.amqp.core.Queue myQueue() {
final boolean isDurable = true;
final boolean isExclusive = false;
final boolean autoDelete = false;
return new org.springframework.amqp.core.Queue(QUEUE_NAME, isDurable, isExclusive, autoDelete);
}
答案 2 :(得分:1)
您需要绑定队列和交换,请尝试下面的RabbitmqConfig
@Configuration
public class RabbitmqConfig {
private static final String QUEUE_NAME = "testqueue";
@Bean
Queue queue() {
return new Queue(QUEUE_NAME, false);
}
@Bean
TopicExchange exchange() {
return new TopicExchange("spring-boot-exchange", true, false);
}
@Bean
Binding binding(Queue queue, TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(QUEUE_NAME);
}
}