@Override public void send(String body) { // Construct a message HazelcastMQMessage msg = new HazelcastMQMessage(); msg.setBody(body); send(msg); }
/** * Converts from a Camel message to a HzMq message. The headers are simply * copied unmodified. The body is mapped by type: * <ul> * <li>If the content type is text/plain, the body is set as a String</li> * <li>all others: the body is set as a byte[] (or null)</li> * </ul> * * @param mqMsg the HzMq message to convert * * @return the new Camel message */ @Override public Message toCamelMessage(HazelcastMQMessage mqMsg) { DefaultMessage camelMsg = new DefaultMessage(); camelMsg.setHeaders((Map) mqMsg.getHeaders().getHeaderMap()); if (mqMsg.getContentType() != null && mqMsg.getContentType().equals( "text/plain")) { camelMsg.setBody(mqMsg.getBodyAsString()); } else { camelMsg.setBody(mqMsg.getBody()); } return camelMsg; } }
HazelcastMQMessage mqMsg = new HazelcastMQMessage(); for (Map.Entry<String, Object> camelHeader : camelHeaders.entrySet()) { if (camelHeader.getValue() instanceof String) { mqMsg.getHeaders().put(camelHeader.getKey(), (String) camelHeader. getValue()); mqMsg.setBody((String) camelBody); mqMsg.setContentType("text/plain"); mqMsg.setBody((byte[]) camelBody); mqMsg.setContentType("application/octet-stream"); mqMsg.setBody((byte[]) null);
/** * Returns the body as a String using the character set encoding in the * content-type header or UTF-8 if no character set is specified in the * headers. If there is no body to the message, null is returned. This is just * a convenience method for {@link #getBody() }. * * @return the body as a string or null */ public String getBodyAsString() { Charset charset = UTF_8; String contentType = getHeaders().get(Headers.CONTENT_TYPE); if (contentType != null && contentType.contains(";")) { String[] parts = contentType.split(";"); for (String part : parts) { if (part.startsWith("charset=")) { charset = Charset.forName(part.substring("charset=".length())); } } } return getBodyAsString(charset); }
@Override public HazelcastMQMessage fromFrame(Frame frame) { HazelcastMQMessage msg = new HazelcastMQMessage(); org.mpilone.yeti.Headers headers = frame.getHeaders(); for (String name : headers.getHeaderNames()) { String value = headers.get(name); msg.getHeaders().put(name, value); } msg.setBody(frame.getBody()); return msg; }
HazelcastMQMessage msg = new HazelcastMQMessage(); msg.setBody("Do some work and get back to me."); msg.setReplyTo(replyDest); msg.setCorrelationId(UUID.randomUUID().toString()); msg.setContentType("text/plain"); String rd = msg.getReplyTo(); String corrId = msg.getCorrelationId(); getBodyAsString(), rd); msg = new HazelcastMQMessage(); msg.setBody("Work is done. You're welcome."); msg.setContentType("text/plain"); msg.setCorrelationId(corrId); log.info("Got reply message [{}].", msg.getBodyAsString());
.createConsumer("/queue/example.dest"); HazelcastMQMessage msg = new HazelcastMQMessage(); msg.setBody("Hello World!"); msg.getBodyAsString(), (endTime - startTime)));
@Override public Frame toFrame(HazelcastMQMessage msg) { FrameBuilder fb = FrameBuilder.command(Command.MESSAGE); fb.body(msg.getBody()); for (String name : msg.getHeaders().getHeaderNames()) { String value = msg.getHeaders().get(name); fb.header(name, value); } return fb.build(); } }
/** * Sets the ID of the message. This is a convenience method for setting the * {@link Headers#MESSAGE_ID} value. * * @param id the ID of the message */ public void setId(String id) { getHeaders().put(Headers.MESSAGE_ID, id); }
String contentType = mqMsg.getContentType(); byte[] body = mqMsg.getBody(); textMsg.setText(new String(mqMsg.getBody(), UTF_8)); for (String headerName : mqMsg.getHeaders().getHeaderNames()) { String headerValue = mqMsg.getHeaders().get(headerName);
@Override public byte[] receiveBody(long timeout, TimeUnit unit) { HazelcastMQMessage msg = receive(timeout, unit); if (msg != null) { return msg.getBody(); } else { return null; } }
/** * Sends a reply message if the exchange has a valid message in the out or * in fields (in that order). * * @param correlationId the optional correlation ID to set on the out going * message * @param replyToDestination the reply to destination * @param camelExchange the exchange containing the message to send as the * reply */ private void sendReply(String correlationId, String replyToDestination, Exchange camelExchange) { Message camelMsg = camelExchange.hasOut() ? camelExchange.getOut() : camelExchange.getIn(); if (camelMsg != null) { try { HazelcastMQMessage msg = messageConverter.fromCamelMessage(camelMsg); msg.getHeaders().remove(org.mpilone.hazelcastmq.core.Headers.REPLY_TO); msg.setDestination(replyToDestination); msg.setCorrelationId(correlationId); mqContext.createProducer(replyToDestination).send(msg); } catch (Throwable ex) { camelExchange.setException(ex); } } } }
msg.getHeaders().put(Headers.EXPIRATION, String.valueOf(System.currentTimeMillis() + timeToLive)); msg.setId("hazelcastmq-" + idGenerator.newId()); msg.setDestination(destination);
msg.setReplyTo(replyToDestination); if (msg.getCorrelationId() == null) { msg.setCorrelationId("CamelHzMq-" + UUID.randomUUID().toString()); replyManager.addPendingReply(msg.getCorrelationId(), requestTimeout, replyQueue); "No reply received for correlation ID %s on reply to " + "destination %s after %d milliseconds.", msg.getCorrelationId(), replyToDestination, requestTimeout));
@Override public void onMessage(HazelcastMQMessage msg) { log.info("Read data: {}", msg.getBodyAsString()); } });
@Override public void onMessage(HazelcastMQMessage msg) { String replyToDestination = msg.getReplyTo(); String correlationId = msg.getCorrelationId(); // Build an exchange. Message camelMsg = messageConverter.toCamelMessage(msg); Exchange camelExchange = getEndpoint().createExchange(); // Change the pattern to out/in if we have a reply destination // and the exchange isn't already out capable. if (replyToDestination != null && !camelExchange.getPattern(). isOutCapable()) { camelExchange.setPattern(ExchangePattern.OutIn); } camelExchange.setIn(camelMsg); try { getProcessor().process(camelExchange); } catch (Throwable e) { camelExchange.setException(e); } if (!camelExchange.isFailed() && replyToDestination != null && camelExchange.getPattern().isOutCapable()) { sendReply(correlationId, replyToDestination, camelExchange); } if (camelExchange.isFailed()) { getExceptionHandler().handleException("Error processing exchange.", camelExchange, camelExchange.getException()); } }
@Override public void onMessage(HazelcastMQMessage msg) { long now = System.currentTimeMillis(); PendingReply pendingReply = null; // Cleanup expired pending replies. if (nextExpiration.get() <= now) { nextExpiration.set(Long.MAX_VALUE); for (Iterator<PendingReply> iter = pendingReplyMap.values().iterator(); iter.hasNext();) { pendingReply = iter.next(); if (pendingReply.getExpiration() <= now) { iter.remove(); } else { setIfLess(nextExpiration, pendingReply.getExpiration()); } } } // Process the incoming reply. String correlationId = msg.getCorrelationId(); if (correlationId != null) { pendingReply = pendingReplyMap.remove(correlationId); } if (pendingReply != null) { pendingReply.getReplyQueue().offer(msg); } else { // TODO: log this } }
HazelcastMQMessage msg = new HazelcastMQMessage(); msg.setBody("Do some work and get back to me."); msg.setReplyTo(replyDest); msg.setCorrelationId(UUID.randomUUID().toString()); msg.setContentType("text/plain"); String rd = msg.getReplyTo(); String corrId = msg.getCorrelationId(); getBodyAsString(), rd); msg = new HazelcastMQMessage(); msg.setBody("Work is done. You're welcome."); msg.setContentType("text/plain"); msg.setCorrelationId(corrId); log.info("Got reply message [{}].", msg.getBodyAsString());
HazelcastMQMessage message = new HazelcastMQMessage(); message.getHeaders().put(key, value); message.setBody(body);