我在这里关注了Java Spring启动的Rabbitmq教程:https://www.rabbitmq.com/tutorials/tutorial-one-spring-amqp.html
我的目标是使用rabbitmq从Spring中的一个微服务接收字符串变量,将字符串附加到该值并将结果传递给API服务器。所以我必须将发送者和接收者代码组合在一个类文件中。
接收队列是python_java,发送队列是java_api。
我的代码 Ampqtutorials application.java
package com.example.amqp.tutorials.amqptutorials;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class AmqpTutorialsApplication {
@Profile("usage_message")
@Bean
public CommandLineRunner usage() {
return new CommandLineRunner() {
@Override
public void run(String... arg0) throws Exception {
System.out.println("This app uses Spring Profiles control its behavior.\n");
System.out.println("Sample usage: java -jar rabbit-tutorials.jar --spring.profiles.active=hello-world,sender");
}
};
}
@Profile("!usage_message")
@Bean
public CommandLineRunner tutorial() {
return new RabbitAmqpTutorialsRunner();
}
public static void main(String[] args) {
SpringApplication.run(AmqpTutorialsApplication.class, args);
}
}
RabbitAmqpTutorialsRunner.java
package com.example.amqp.tutorials.amqptutorials;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ConfigurableApplicationContext;
public class RabbitAmqpTutorialsRunner implements CommandLineRunner {
@Value("${tutorial.client.duration:0}")
private int duration;
@Autowired
private ConfigurableApplicationContext ctx;
@Override
public void run(String... arg0) throws Exception {
System.out.println("Ready ... running for " + duration + "ms");
Thread.sleep(duration);
ctx.close();
}
}
Tut1Config.java
package com.example.amqp.tutorials.amqptutorials.tut1;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Profile({"tut1","hello-world"})
@Configuration
public class Tut1Config {
@Bean
public Queue hello() {
return new Queue("python_java");
}
@Profile("receiver")
@Bean
public Tut1Receiver receiver() {
return new Tut1Receiver();
}
@Profile("sender")
@Bean
public Tut1Sender sender() {
return new Tut1Sender();
}
}
Tut1Sender.java
package com.example.amqp.tutorials.amqptutorials.tut1;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
public class Tut1Sender {
@Autowired
private RabbitTemplate template;
@Scheduled(fixedDelay = 1000, initialDelay = 500)
public void send() {
String message = "Hello World!";
this.template.convertAndSend("java_api", message);
System.out.println(" [x] Sent '" + message + "'");
}
}
Tut1Receiver.java
package com.example.amqp.tutorials.amqptutorials.tut1;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
@RabbitListener(queues = "python_java")
public class Tut1Receiver {
@RabbitHandler
public void receive(String in) {
System.out.println(" [x] Received '" + in + "'");
}
}
那么如何将接收和发送功能放在同一个文件中?