Spring AMQP 项目不执行任何操作
Spring AMQP project doesn't do anything
我已经为这个任务纠结好几天了,所以我决定在这里问一个问题。
好吧,我有一个远程 RabbitMQ 服务器,我只有发送和接收消息的权限。这意味着我无法创建任何东西。
所以,我想接收and/or发送的消息。但由于某种原因我不能这样做。应用程序启动、通道创建、bean 初始化等。但是接收方和发送方什么都不做。
我几乎可以肯定问题很简单,我会认为我在回答之后是个白痴,但现在我被卡住了。
代码如下。主要 class 是标准的,没有任何变化。
配置类:
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$53f575] 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)
然后什么也没发生。问题是我做错了什么,为什么它不能按我想要的方式工作以及如何解决它?
我知道复制粘贴整个项目并要求解决我所有的问题并不是一个很好的做法,我对此感到抱歉,但我目前没有看到任何其他方法可以使我的代码正常工作.我很乐意得到任何帮助。
要能够使用 @Scheduled
注释,您必须首先启用使用 @EnableScheduling
的调度。
尝试将其添加到您的配置中 class:
@Configuration
@EnableScheduling
public class TestConfiguration { ... }
解决此问题后,您可能还需要查看 wargre 的评论。
我已经为这个任务纠结好几天了,所以我决定在这里问一个问题。
好吧,我有一个远程 RabbitMQ 服务器,我只有发送和接收消息的权限。这意味着我无法创建任何东西。
所以,我想接收and/or发送的消息。但由于某种原因我不能这样做。应用程序启动、通道创建、bean 初始化等。但是接收方和发送方什么都不做。
我几乎可以肯定问题很简单,我会认为我在回答之后是个白痴,但现在我被卡住了。
代码如下。主要 class 是标准的,没有任何变化。
配置类:
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$53f575] 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)
然后什么也没发生。问题是我做错了什么,为什么它不能按我想要的方式工作以及如何解决它?
我知道复制粘贴整个项目并要求解决我所有的问题并不是一个很好的做法,我对此感到抱歉,但我目前没有看到任何其他方法可以使我的代码正常工作.我很乐意得到任何帮助。
要能够使用 @Scheduled
注释,您必须首先启用使用 @EnableScheduling
的调度。
尝试将其添加到您的配置中 class:
@Configuration
@EnableScheduling
public class TestConfiguration { ... }
解决此问题后,您可能还需要查看 wargre 的评论。