我已经在这项任务上挣扎了几天,所以我决定在这里问一个问题。
嗯,我有一个远程RabbitMQ服务器,并且只具有发送和接收消息的权限。这意味着我无法创建任何东西。
所以,我想接收和/或发送消息。但是由于某些原因我无法做到。该应用程序启动,创建通道,初始化bean等。但是接收者和发送者根本不执行任何操作。
我几乎可以肯定问题很简单,我会在回答后认为我是个白痴,但现在我被困住了。
代码在下面。主要类是标准,没有任何更改。
ConfigurationClass:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TestConfiguration {
@Bean
public Sender sender() {
return new Sender();
}
@Bean
public Receiver receiver() {
return new Receiver();
}
}
发件人:
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
public class Sender {
@Autowired
private RabbitTemplate template;
@Scheduled(fixedDelay = 1000, initialDelay = 500)
public void send() {
String message = "Hello World!";
this.template.convertAndSend("ExchangeName","RoutingKey", message);
System.out.println(" [x] Sent '" + message + "'");
}
}
接收器:
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
@RabbitListener(queues = "QueueName")
public class Receiver {
@RabbitHandler
public void receive(String in) {
System.out.println(" [x] Received '" + in + "'");
}
}
application.properties:
spring.rabbitmq.host=host.com
spring.rabbitmq.port=5673
spring.rabbitmq.username=username
spring.rabbitmq.password=password
spring.rabbitmq.ssl.enabled=true
日志:
2018-09-02 23:18:50.409 INFO 9092 --- [ main] com.application.Application : Starting Application on WORK-PC with PID 9092 (started by Victor in ...\application)
2018-09-02 23:18:50.413 INFO 9092 --- [ main] com.application.Application : No active profile set, falling back to default profiles: default
2018-09-02 23:18:50.473 INFO 9092 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@52af26ee: startup date [Sun Sep 02 23:18:50 KRAT 2018]; root of context hierarchy
2018-09-02 23:18:51.138 INFO 9092 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration' of type [org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration$$EnhancerBySpringCGLIB$$6653f575] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-09-02 23:18:51.908 INFO 9092 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-09-02 23:18:51.916 INFO 9092 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'rabbitConnectionFactory' has been autodetected for JMX exposure
2018-09-02 23:18:51.918 INFO 9092 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Located managed bean 'rabbitConnectionFactory': registering with JMX server as MBean [org.springframework.amqp.rabbit.connection:name=rabbitConnectionFactory,type=CachingConnectionFactory]
2018-09-02 23:18:51.960 INFO 9092 --- [ main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 2147483647
2018-09-02 23:18:51.970 INFO 9092 --- [cTaskExecutor-1] o.s.a.r.c.CachingConnectionFactory : Attempting to connect to: [host.com:5673]
2018-09-02 23:18:52.718 INFO 9092 --- [cTaskExecutor-1] o.s.a.r.c.CachingConnectionFactory : Created new connection: rabbitConnectionFactory#7a356a0d:0/SimpleConnection@25e05629 [delegate=amqp://username@159.69.17.11:5673/, localPort= 53053]
2018-09-02 23:18:53.158 INFO 9092 --- [ main] com.application.Application : Started Application in 3.108 seconds (JVM running for 3.561)
然后什么也没有发生。问题是我在做什么错,为什么它不能按我的意愿工作以及如何解决?
我知道复制粘贴整个项目并要求解决我的所有问题并不是一个很好的做法,对此我感到很抱歉,但是目前我看不到任何使我的代码正常工作的方法。我将很高兴获得任何帮助。
答案 0 :(得分:1)
要使用@Scheduled
批注,必须首先使用@EnableScheduling
启用计划。
尝试将其添加到您的配置类中:
@Configuration
@EnableScheduling
public class TestConfiguration { ... }
修复此问题后,您可能还需要查看wargre的评论。