Spring 春天如何接收和回复
我正在尝试部署RPC(请求/应答模式),并且在服务器端使用RabbitMQ和Spring,因为我需要动态使用者。我可以使用Spring 春天如何接收和回复,spring,rabbitmq,spring-rabbit,Spring,Rabbitmq,Spring Rabbit,我正在尝试部署RPC(请求/应答模式),并且在服务器端使用RabbitMQ和Spring,因为我需要动态使用者。我可以使用SimpleMessageListenerContainer配置动态消费者,但我不知道如何回复我的消息 以下是我的类配置: @Configuration public class dynamicConsumerConfig { private static Properties prop = new Properties(); public stati
SimpleMessageListenerContainer
配置动态消费者,但我不知道如何回复我的消息
以下是我的类配置:
@Configuration
public class dynamicConsumerConfig {
private static Properties prop = new Properties();
public static void setPropValues() throws IOException {
File configFile = new File("src/main/resources/config.properties");
InputStream inStream = new FileInputStream(configFile.getAbsolutePath());
prop.load(inStream);
}
@Bean
public Queue slowQueue() {
return new Queue("slowQueue");
}
@Bean
public Queue fastQueue() {
return new Queue("fastQueue");
}
@Bean
public DirectExchange exchange1() {
return new DirectExchange("pdfqueues");
}
@Bean
public Binding slowBind(DirectExchange exchange, Queue slowQueue) {
return BindingBuilder.bind(slowQueue)
.to(exchange)
.with("slow");
}
@Bean
public Binding fastBind(DirectExchange exchange, Queue fastQueue) {
return BindingBuilder.bind(fastQueue)
.to(exchange)
.with("fast");
}
@Bean
public ConnectionFactory connect() throws IOException {
setPropValues();
CachingConnectionFactory connection = new CachingConnectionFactory();
connection.setHost(prop.getProperty("HOST"));
connection.setUsername(prop.getProperty("USER"));
connection.setPassword(prop.getProperty("PASS"));
connection.setPort(Integer.parseInt(prop.getProperty("PORT")));
return connection;
}
@Bean
public SimpleMessageListenerContainer container1(ConnectionFactory connection) throws IOException {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
setPropValues();
container.setConnectionFactory(connection);
container.setQueueNames("slowQueue");
container.setMessageListener(firstListener());
container.setMaxConcurrentConsumers(8);
container.setConcurrentConsumers(1);
container.setConsecutiveActiveTrigger(1);
container.setConsecutiveIdleTrigger(1);
container.setTxSize(1);
container.setPrefetchCount(1);
return container;
}
@Bean
public MessageListener firstListener()
{
return new MessageListener() {
@Override
public void onMessage(Message message) {
PdfBoxService pdfboxservice = new PdfBoxService(prop.getProperty("tmpPath"),prop.getProperty("imagicPath"),prop.getProperty("resources"),
prop.getProperty("tessdata"),prop.getProperty("languages"));
String picture = new String(message.getBody(), StandardCharsets.UTF_8);
List<ImagePair> lip = null;
try {
lip = new ArrayList<ImagePair>();
lip.add(new ImagePair("JPG", picture));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ByteArrayOutputStream output= pdfboxservice.ImgToPdf(lip, false, false, false, 1, 1);
} catch (IOException | InterruptedException | TransformerException | BadFieldValueException
| TesseractException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}
@配置
公共类dynamicConsumerConfig{
私有静态属性prop=新属性();
公共静态void setPropValues()引发IOException{
File configFile=新文件(“src/main/resources/config.properties”);
InputStream inStream=新文件InputStream(configFile.getAbsolutePath());
道具荷载(河道内);
}
@豆子
公共队列slowQueue(){
返回新队列(“慢队列”);
}
@豆子
公共队列fastQueue(){
返回新队列(“快速队列”);
}
@豆子
公共DirectExchange交换1(){
返回新的DirectExchange(“pdfqueues”);
}
@豆子
公共绑定slowBind(DirectExchange交换、队列slowQueue){
返回BindingBuilder.bind(slowQueue)
.至(交换)
.用(“慢”);
}
@豆子
公共绑定快速绑定(DirectExchange交换、队列快速队列){
返回BindingBuilder.bind(fastQueue)
.至(交换)
.使用(“快速”);
}
@豆子
public ConnectionFactory connect()引发IOException{
setPropValues();
CachingConnectionFactory连接=新的CachingConnectionFactory();
connection.setHost(prop.getProperty(“主机”));
connection.setUsername(prop.getProperty(“用户”));
connection.setPassword(prop.getProperty(“PASS”);
connection.setPort(Integer.parseInt(prop.getProperty(“PORT”));
回路连接;
}
@豆子
公共SimpleMessageListenerContainer容器1(ConnectionFactory连接)引发IOException{
SimpleMessageListenerContainer容器=新SimpleMessageListenerContainer();
setPropValues();
容器。设置连接工厂(连接);
container.setQueueNames(“slowQueue”);
setMessageListener(firstListener());
container.setMaxConcurrentConsumers(8);
容器。setConcurrentConsumers(1);
container.setConcertiveActiveTrigger(1);
容器。SetConcertiveIdleTrigger(1);
container.setTxSize(1);
容器。设置预取计数(1);
返回容器;
}
@豆子
public MessageListener firstListener()
{
返回新的MessageListener(){
@凌驾
消息(消息消息)上的公共无效{
PdfBoxService PdfBoxService=新的PdfBoxService(prop.getProperty(“tmpPath”)、prop.getProperty(“imagicPath”)、prop.getProperty(“资源”),
prop.getProperty(“数据”)、prop.getProperty(“语言”);
字符串图片=新字符串(message.getBody(),StandardCharsets.UTF_8);
List lip=null;
试一试{
lip=新的ArrayList();
添加(新图像对(“JPG”,图片));
}捕获(FileNotFoundException e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
试一试{
ByteArrayOutputStream输出=pdfboxservice.ImgToPdf(lip,false,false,false,1,1);
}捕获(IOException | InterruptedException | TransformerException | BadFieldValueException)
|TesseRacte){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
};
}
在函数firstListener()
中,我得到消息。在本例中是一张图片。图片从JPG转换为PDF。PDF存储在output
变量中
我需要在其他队列中回复此输出
,但我没有工具来完成。
我认为我的代码是一个糟糕的模式,但我不知道如何使用SimpleMessageListenerContainer
使用带有返回结果的POJO方法的RPC模式,而不是自己实现MessageListener
从2.0版开始,提供了方便的功能界面
:
@functioninterface
公共接口ReplyingMessageListener{
R handleMessage(T);
}
这有助于使用Java 8 lamdas方便地配置适配器:
newmessagelisteneradapter((ReplyingMessageListener)数据->{
...
返回结果;
}));
将a与返回结果的POJO方法一起使用,而不是自己实现MessageListener
从2.0版开始,提供了方便的功能界面
:
@functioninterface
公共接口ReplyingMessageListener{
R handleMessage(T);
}
这有助于使用Java 8 lamdas方便地配置适配器:
newmessagelisteneradapter((ReplyingMessageListener)数据->{
...
返回结果;
}));
您已经创建了两个绑定,如何区分这两个绑定,并知道在哪个位置插入哪个绑定(fastQueue在fast中,slowQueue在slow中)……我看不到编译器可以区分这两个绑定的方法您已经创建了两个绑定,如何区分这两个绑定并知道在哪个位置插入哪个绑定(fastQueue在fast中,slowQueue在slow中)……我看不到编译器可以区分这两个绑定的方法
@FunctionalInterface
public interface ReplyingMessageListener<T, R> {
R handleMessage(T t);
}
new MessageListenerAdapter((ReplyingMessageListener<String, String>) data -> {
...
return result;
}));