简体   繁体   English

ActiveMQ:使用特定的流

[英]ActiveMQ: consume specific stream

I have a JAX-RS endpoint that consumes a JSON payload: 我有一个使用JSON负载的JAX-RS端点:



    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response postEnvelope(final InputStream is) {
    ...

I want to stream this JSON InputStream into ActiveMQ: 我想将此JSON InputStream流式传输到ActiveMQ:



    ...
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination destination = session.createQueue("envelopes");
    MessageProducer producer = session.createProducer(destination);
    producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    StreamMessage streamMessage = session.createStreamMessage();
    byte[] bytes = new byte[1024 * 100];
    for (int count; (count = is.read(bytes)) > 0; ) {
        streamMessage.clearBody();
        streamMessage.writeBytes(bytes, 0, count);
        producer.send(streamMessage);
    }
    ...

I then want another thread to consume the ActiveMQ JSON stream and write it to an OutputStream: 然后,我希望另一个线程使用ActiveMQ JSON流并将其写入OutputStream:



    ...
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination envelopesQueue = session.createQueue("envelopes");
    MessageConsumer consumer = session.createConsumer(envelopesQueue);
    Message message = consumer.receive();
    if (message instanceof StreamMessage) {
        do {
            StreamMessage streamMessage = (StreamMessage) message;
            byte[] bytes = new byte[1024 * 100];
            for (int count; (count = streamMessage.readBytes(bytes)) > 0; ) {
                out.write(bytes, 0, count);
            }
        } while ((message = consumer.receive(2500)) != null);
    }
    ...

My Question is; 我的问题是; How do I make sure my ActiveMQ consumer only receives messages associated with a specific JSON stream? 如何确保我的ActiveMQ使用者接收与特定JSON流关联的消息? (ie: If two JSON payloads are posted to the REST endpoint at the exact same time, then how do I prevent them both from being written to the same OutputStream). (即:如果将两个JSON有效负载恰好同时发布到REST端点,那么如何防止将它们都写入相同的OutputStream)。

I'm going to answer my own question; 我要回答我自己的问题; I'm no longer using the StreamMessage API. 我不再使用StreamMessage API。 Instead my POST endpoint is saving the input stream to a file, and I'm sending a TextMessage containing the URL of where to retrieve the file. 相反,我的POST端点将输入流保存到文件中,并且我正在发送一个TextMessage,其中包含在何处检索文件的URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM