我正在尝试部署RPC(请求/答复模式),并且在服务器端使用RabbitMQ和Spring,因为我需要动态使用者。我可以使用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();
}
}
};
}
在功能firstListener()
中,我收到了消息。在这种情况下是一张图片。图片从JPG转换为PDF。 PDF存储在output
变量中。
我需要在其他队列中回复此output
,但是我没有执行此操作的工具。
我认为我的代码是错误的模式,但是我不知道如何使用SimpleMessageListenerContainer
与动态使用者进行RPC模式。
答案 0 :(得分:1)
将MessageListenerAdapter
与POJO方法结合使用会返回结果,而不是自己实现MessageListener
。
从2.0版开始,提供了方便的
FunctionalInterface
:
@FunctionalInterface
public interface ReplyingMessageListener<T, R> {
R handleMessage(T t);
}
这有助于使用Java 8 lamdas方便地配置适配器:
new MessageListenerAdapter((ReplyingMessageListener<String, String>) data -> {
...
return result;
}));