protected void sendReceipt(StompFrame command) { final String receiptId = command.getHeaders().get(Stomp.Headers.RECEIPT_REQUESTED); if (receiptId != null) { StompFrame sc = new StompFrame(); sc.setAction(Stomp.Responses.RECEIPT); sc.setHeaders(new HashMap<String, String>(1)); sc.getHeaders().put(Stomp.Headers.Response.RECEIPT_ID, receiptId); try { sendToStomp(sc); } catch (IOException e) { LOG.warn("Could not send a receipt for {}", command, e); } } }
public void marshal(Object command, DataOutput os) throws IOException { StompFrame stomp = (org.apache.activemq.transport.stomp.StompFrame)command; StringBuffer buffer = new StringBuffer(); buffer.append(stomp.getAction()); buffer.append(Stomp.NEWLINE); // Output the headers. for (Iterator iter = stomp.getHeaders().entrySet().iterator(); iter.hasNext();) { Map.Entry entry = (Map.Entry)iter.next(); buffer.append(entry.getKey()); buffer.append(Stomp.Headers.SEPERATOR); buffer.append(entry.getValue()); buffer.append(Stomp.NEWLINE); } // Add a newline to seperate the headers from the content. buffer.append(Stomp.NEWLINE); os.write(buffer.toString().getBytes("UTF-8")); os.write(stomp.getContent()); os.write(END_OF_FRAME); }
public void connect(HashMap<String, String> headers) throws Exception { StompFrame frame = new StompFrame("CONNECT", headers); sendFrame(frame.format()); StompFrame connect = receive(); if (!connect.getAction().equals(Stomp.Responses.CONNECTED)) { throw new Exception ("Not connected: " + connect.getBody()); } }
public void disconnect(String receiptId) throws Exception { StompFrame frame = new StompFrame("DISCONNECT"); if (receiptId != null && !receiptId.isEmpty()) { frame.getHeaders().put(Stomp.Headers.RECEIPT_REQUESTED, receiptId); } sendFrame(frame.format()); }
public StompFrame convertMessage(ProtocolConverter converter, ActiveMQMessage message) throws IOException, JMSException { StompFrame command = new StompFrame(); command.setAction(Stomp.Responses.MESSAGE); Map<String, String> headers = new HashMap<String, String>(25); command.setHeaders(headers); FrameTranslator.Helper.copyStandardHeadersFromMessageToFrame(converter, message, command, this); if (message.getDataStructureType() == ActiveMQTextMessage.DATA_STRUCTURE_TYPE) { ActiveMQTextMessage msg = (ActiveMQTextMessage)message.copy(); command.setContent(msg.getText().getBytes("UTF-8")); } else if (message.getDataStructureType() == ActiveMQBytesMessage.DATA_STRUCTURE_TYPE) { ActiveMQBytesMessage msg = (ActiveMQBytesMessage)message.copy(); msg.setReadOnlyBody(true); byte[] data = new byte[(int)msg.getBodyLength()]; msg.readBytes(data); headers.put(Stomp.Headers.CONTENT_LENGTH, "" + data.length); command.setContent(data); } else if (message.getDataStructureType() == ActiveMQMessage.DATA_STRUCTURE_TYPE && AdvisorySupport.ADIVSORY_MESSAGE_TYPE.equals(message.getType())) { FrameTranslator.Helper.copyStandardHeadersFromMessageToFrame( converter, message, command, this); String body = marshallAdvisory(message.getDataStructure()); command.setContent(body.getBytes("UTF-8")); } return command; }
public void unsubscribe(String destination, HashMap<String, String> headers) throws Exception { if (headers == null) { headers = new HashMap<String, String>(); } headers.put("destination", destination); StompFrame frame = new StompFrame("UNSUBSCRIBE", headers); sendFrame(frame.format()); }
@Override public ActiveMQMessage convertFrame(ProtocolConverter converter, StompFrame command) throws JMSException, ProtocolException { final Map<?, ?> headers = command.getHeaders(); final ActiveMQMessage msg; ActiveMQTextMessage text = new ActiveMQTextMessage(); try { ByteArrayOutputStream bytes = new ByteArrayOutputStream(command.getContent().length + 4); DataOutputStream data = new DataOutputStream(bytes); data.writeInt(command.getContent().length); data.write(command.getContent()); text.setContent(bytes.toByteSequence()); data.close(); } else if(intendedType.equalsIgnoreCase("bytes")) { ActiveMQBytesMessage byteMessage = new ActiveMQBytesMessage(); byteMessage.writeBytes(command.getContent()); msg = byteMessage; } else { headers.remove(Stomp.Headers.CONTENT_LENGTH); ActiveMQBytesMessage bm = new ActiveMQBytesMessage(); bm.writeBytes(command.getContent()); msg = bm; } else { ActiveMQTextMessage text = new ActiveMQTextMessage(); try { ByteArrayOutputStream bytes = new ByteArrayOutputStream(command.getContent().length + 4); DataOutputStream data = new DataOutputStream(bytes); data.writeInt(command.getContent().length);
public String marshalToString(StompFrame stomp) throws IOException { if (stomp.getAction().equals(Stomp.Commands.KEEPALIVE)) { return String.valueOf((char)Stomp.BREAK); } StringBuilder buffer = new StringBuilder(); marshalHeaders(stomp, buffer); if (stomp.getContent() != null) { String contentString = new String(stomp.getContent(), "UTF-8"); buffer.append(contentString); } buffer.append('\u0000'); return buffer.toString(); }
private StringBuilder marshalHeaders(StompFrame stomp, StringBuilder buffer) throws IOException { buffer.append(stomp.getAction()); buffer.append(Stomp.NEWLINE); // Output the headers. for (Map.Entry<String, String> entry : stomp.getHeaders().entrySet()) { buffer.append(entry.getKey()); buffer.append(Stomp.Headers.SEPERATOR); buffer.append(encodeHeader(entry.getValue())); buffer.append(Stomp.NEWLINE); } // Add a newline to separate the headers from the content. buffer.append(Stomp.NEWLINE); return buffer; }
@Override void onMessageDispatch(MessageDispatch md, String ackId) throws IOException, JMSException { if (md.getMessage() != null) { super.onMessageDispatch(md, ackId); } else { StompFrame browseDone = new StompFrame(Stomp.Responses.MESSAGE); browseDone.getHeaders().put(Stomp.Headers.Message.SUBSCRIPTION, this.getSubscriptionId()); browseDone.getHeaders().put(Stomp.Headers.Message.BROWSER, "end"); browseDone.getHeaders().put(Stomp.Headers.Message.DESTINATION, protocolConverter.findTranslator(null).convertDestination(protocolConverter, this.destination)); browseDone.getHeaders().put(Stomp.Headers.Message.MESSAGE_ID, "0"); protocolConverter.sendToStomp(browseDone); } }
final Map<String, String> headers = command.getHeaders(); connectionInfo.setUserName(login); connectionInfo.setPassword(passcode); connectionInfo.setTransportContext(command.getTransportContext());
public void ack(StompFrame frame) throws Exception { ack(frame.getHeaders().get("message-id"), null); }
void onMessageDispatch(MessageDispatch md, String ackId) throws IOException, JMSException { ActiveMQMessage message = (ActiveMQMessage)md.getMessage(); if (ackMode.equals(CLIENT_ACK) || ackMode.equals(INDIVIDUAL_ACK)) { synchronized (this) { dispatchedMessage.put(message.getMessageId(), md); } } else if (ackMode.equals(AUTO_ACK)) { MessageAck ack = new MessageAck(md, MessageAck.STANDARD_ACK_TYPE, 1); protocolConverter.getStompTransport().sendToActiveMQ(ack); } boolean ignoreTransformation = false; if (transformation != null && !( message instanceof ActiveMQBytesMessage ) ) { message.setReadOnlyProperties(false); message.setStringProperty(Stomp.Headers.TRANSFORMATION, transformation); } else { if (message.getStringProperty(Stomp.Headers.TRANSFORMATION) != null) { ignoreTransformation = true; } } StompFrame command = protocolConverter.convertMessage(message, ignoreTransformation); command.setAction(Stomp.Responses.MESSAGE); if (subscriptionId != null) { command.getHeaders().put(Stomp.Headers.Message.SUBSCRIPTION, subscriptionId); } if (ackId != null) { command.getHeaders().put(Stomp.Headers.Message.ACK_ID, ackId); } protocolConverter.getStompTransport().sendToStomp(command); }
StompConnection connection = new StompConnection(); connection.open("localhost", 61613); connection.connect("system", "manager"); StompFrame connect = connection.receive(); if (!connect.getAction().equals(Stomp.Responses.CONNECTED)) { throw new Exception ("Not connected"); } connection.begin("tx1"); connection.send("/queue/test", "message1", "tx1", null); connection.send("/queue/test", "message2", "tx1", null); connection.commit("tx1"); connection.subscribe("/queue/test", Subscribe.AckModeValues.CLIENT); connection.begin("tx2"); StompFrame message = connection.receive(); System.out.println(message.getBody()); connection.ack(message, "tx2"); message = connection.receive(); System.out.println(message.getBody()); connection.ack(message, "tx2"); connection.commit("tx2"); connection.disconnect();
public StompFrame apply() throws IOException { Buffer content = readUntil((byte) 0); if (content != null) { nextDecodeAction = read_action; frame.setContent(BufferSupport.chomp(content).getBytes()); return frame; } else { return null; } } };
protected void processCommand() throws Exception { StompFrame frame = new StompFrame(action, headers, currentCommand.toByteArray()); transport.doConsume(frame); processedHeaders = false; awaitingCommandStart = true; currentCommand.reset(); contentLength = -1; frameSize.set(0); }
@Override public StompFrame convertMessage(ProtocolConverter converter, ActiveMQMessage message) throws IOException, JMSException { StompFrame command = new StompFrame(); command.setAction(Stomp.Responses.MESSAGE); Map<String, String> headers = new HashMap<String, String>(25); command.setHeaders(headers); byte[] content = new byte[msgContent.getLength() - 4]; System.arraycopy(msgContent.data, 4, content, 0, content.length); command.setContent(content); String messageText = msg.getText(); if (messageText != null) { command.setContent(msg.getText().getBytes("UTF-8")); command.setContent(data);
public void abort(String transaction) throws Exception { HashMap<String, String> headers = new HashMap<String, String>(); headers.put("transaction", transaction); StompFrame frame = new StompFrame("ABORT", headers); sendFrame(frame.format()); }
public ActiveMQMessage convertFrame(ProtocolConverter converter, StompFrame command) throws JMSException, ProtocolException { final Map headers = command.getHeaders(); final ActiveMQMessage msg; ActiveMQTextMessage text = new ActiveMQTextMessage(); try { text.setText(new String(command.getContent(), "UTF-8")); } catch (Throwable e) { throw new ProtocolException("Text could not bet set: " + e, false, e); } else if(intendedType.equalsIgnoreCase("bytes")) { ActiveMQBytesMessage byteMessage = new ActiveMQBytesMessage(); byteMessage.writeBytes(command.getContent()); msg = byteMessage; } else { headers.remove(Stomp.Headers.CONTENT_LENGTH); ActiveMQBytesMessage bm = new ActiveMQBytesMessage(); bm.writeBytes(command.getContent()); msg = bm; } else { ActiveMQTextMessage text = new ActiveMQTextMessage(); try { text.setText(new String(command.getContent(), "UTF-8")); } catch (Throwable e) { throw new ProtocolException("Text could not bet set: " + e, false, e);
public String marshalToString(StompFrame stomp) throws IOException { if (stomp.getAction().equals(Stomp.Commands.KEEPALIVE)) { return String.valueOf((char)Stomp.BREAK); } StringBuilder buffer = new StringBuilder(); marshalHeaders(stomp, buffer); if (stomp.getContent() != null) { String contentString = new String(stomp.getContent(), "UTF-8"); buffer.append(contentString); } buffer.append('\u0000'); return buffer.toString(); }