我有一个消耗JSON有效负载的JAX-RS端点:
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response postEnvelope(final InputStream is) {
...
我想将此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);
}
...
然后我希望另一个线程使用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);
}
...
我的问题是;如何确保我的ActiveMQ使用者仅接收与特定JSON流关联的消息? (即:如果两个JSON有效负载在同一时间发布到REST端点,那么如何防止它们被写入同一个OutputStream)。
答案 0 :(得分:0)
我要回答我自己的问题;我不再使用StreamMessage API了。相反,我的POST端点将输入流保存到文件中,我发送的TextMessage包含检索文件的位置的URL。