我有两个Springboot应用程序,我正在使用RabbitMQ在队列上传达消息。构建一个应用程序以发送消息,另一个应用程序聆听已发送消息。每个应用程序由一个@springbootapplication文件组成,该文件在属性级别具有一个@Autowiend依赖关系(一个应用程序具有另一个应用程序,另一个应用程序具有侦听器),并且每个应用程序都有一个单独一个有接收器)。
由于某种原因,发件人应用程序对注射没有任何问题,但是,即使BEAN在我的应用程序上下文中,接收器应用程序也不会在@Autowire注射。我正在使用示例应用程序作为一种用Springboot和SpringBoot和Microservices向我们的公司展示RabbitMQ/SpringAMQP的手段。以下是发件人申请的代码,然后是接收器申请。如果我将接收器应用程序更改为使用Setter注入,则可以正常工作,我很好奇为什么其中一个作品而另一个作品却没有。接收器应用程序在接收器上爆炸。Receive()以其主要方法的呼叫:
线程中的异常" main" java.lang.nullpointerexception 在com.bettercloud.springamqpapplication.main(springamqpapplication.java:17)
接收器应用程序:
@SpringBootApplication
public class SpringAmqpApplication {
@Autowired
static Recv receiver;
public static void main(String[] args) throws IOException,InterruptedException {
SpringApplication.run(SpringAmqpApplication.class, args);
receiver.receive();
}
}
@Configuration
public class Config {
@Bean
public Recv recv(){
return new Recv();
}
}
public class Recv {
private final static String QUEUE_NAME = "task_queue";
public void receive()
throws java.io.IOException,
InterruptedException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
channel.basicQos(1);
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(QUEUE_NAME, false, consumer);
while (true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
String message = new String(delivery.getBody());
System.out.println(" [x] Received '" + message + "'");
doWork(message);
System.out.println(" [x] Done");
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
}
}
private static void doWork(String task) throws InterruptedException {
for (char ch: task.toCharArray()) {
if (ch == '.') Thread.sleep(1000);
}
}
}
发件人应用程序:
@SpringBootApplication
public class SpringAmqpProducerApplication {
@Autowired
static Send sender;
public static void main(String[] args) throws IOException {
SpringApplication.run(SpringAmqpProducerApplication.class, args);
sender.send(null);
}
}
@Configuration
public class Config {
@Bean
public Send send(){
return new Send();
}
}
public class Send {
private final static String QUEUE_NAME = "task_queue";
public static void send(String[] argv)
throws java.io.IOException {
Connection connection = null;
Channel channel = null;
try {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
connection = factory.newConnection();
channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
String message = getMessage(argv);
channel.basicPublish("", QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
System.out.println(" [x] Sent '" + message + "'");
} finally {
channel.close();
connection.close();
}
}
private static String getMessage(String[] strings){
if (strings == null || strings.length < 1)
return "Hello World!";
return joinStrings(strings, " ");
}
private static String joinStrings(String[] strings, String delimiter) {
int length = strings.length;
if (length == 0) return "";
StringBuilder words = new StringBuilder(strings[0]);
for (int i = 1; i < length; i++) {
words.append(delimiter).append(strings[i]);
}
return words.toString();
}
}
我认为注射根本不起作用,因为您试图注入静态场,而静态场不适用于弹簧。从您的字段中删除静态标识符(从您的方法中,因为也没有理由它们也静态),并且您的应用程序应该正常工作。
发件人工作,因为发送方法是静态的,因此您不需要对象来调用该方法。
不确定这是否会有所帮助,但是发送类中的send()方法是静态的,而接收类的接收()方法则不是。