我正在尝试在我的[Spring]应用程序中实现Rabbitmq,而不是spring boot。所以我添加了此配置
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitConfiguration {
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory =
new CachingConnectionFactory("localhost");
return connectionFactory;
}
@Bean
public AmqpAdmin amqpAdmin() {
return new RabbitAdmin(connectionFactory());
}
@Bean
public RabbitTemplate rabbitTemplate() {
return new RabbitTemplate(connectionFactory());
}
@Bean
public Queue myQueue() {
return new Queue("MyQueue");
}
}
然后从我使用过的服务类别中:-
public void sendViaTemplate(String msg){
ApplicationContext context =
new AnnotationConfigApplicationContext(RabbitConfiguration.class);
RabbitTemplate template = context.getBean(RabbitTemplate.class);
template.convertAndSend(QUEUE_NAME,"Hello from template "+msg);
}
@RabbitListener(queues = "MyQueue")
public void ListenToMyQueue( String in){
System.out.println("New Msg arrived"+in);
}
convertAndSend似乎按预期工作,但是当将消息推送到队列中时,应该在将新元素插入队列中时自动执行ListenToMyQueue,对吗?为什么这不起作用?
我正在尝试在Spring应用程序中实现Rabbitmq,而不是spring boot。因此,我添加了此配置import org.springframework.amqp.core.AmqpAdmin;导入org.springframework.amqp.core.Queue; ...
SimpleRabbitListenerContainerFactory
类中实现RabbitConfiguration
。