簡體   English   中英

如何在Spring上接收和回復

[英]How to receive and reply on Spring

我正在嘗試部署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模式。

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;
}));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM