我已经困扰了这项任务好几天了,所以我决定在这里问一个问题。
好吧,我有一个远程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://[email protected]: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)
然后什么都没发生。问题是我做错了什么,为什么它不能按我的意愿工作以及如何修复它?
我知道复制整个项目并要求解决我的所有问题并不是一个很好的做法,我很抱歉,但我没有看到任何不同的方法让我的代码目前正常工作。我很乐意得到任何帮助。
为了能够使用@Scheduled
注释,您必须首先使用@EnableScheduling
启用调度。
尝试将其添加到配置类:
@Configuration
@EnableScheduling
public class TestConfiguration { ... }
一旦解决了这个问题,你可能还需要看一下wargre的评论。