/** * Map the given object to a {@link BytesMessage}. * @param object the object to be mapped * @param session current JMS session * @param objectWriter the writer to use * @return the resulting message * @throws JMSException if thrown by JMS methods * @throws IOException in case of I/O errors * @since 4.3 * @see Session#createBytesMessage */ protected BytesMessage mapToBytesMessage(Object object, Session session, ObjectWriter objectWriter) throws JMSException, IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); OutputStreamWriter writer = new OutputStreamWriter(bos, this.encoding); objectWriter.writeValue(writer, object); BytesMessage message = session.createBytesMessage(); message.writeBytes(bos.toByteArray()); if (this.encodingPropertyName != null) { message.setStringProperty(this.encodingPropertyName, this.encoding); } return message; }
@Test public void toBytesMessage() throws Exception { BytesMessage bytesMessageMock = mock(BytesMessage.class); Date toBeMarshalled = new Date(); given(sessionMock.createBytesMessage()).willReturn(bytesMessageMock); converter.toMessage(toBeMarshalled, sessionMock); verify(bytesMessageMock).setStringProperty("__encoding__", "UTF-8"); verify(bytesMessageMock).setStringProperty("__typeid__", Date.class.getName()); verify(bytesMessageMock).writeBytes(isA(byte[].class)); }
public Message getMessage(QueueSession queueSession) throws JMSException { BytesMessage bytesMessage = queueSession.createBytesMessage(); bytesMessage.setStringProperty("email", this.email); bytesMessage.setStringProperty("language", this.language); bytesMessage.setStringProperty("mimetype", this.mimetype); bytesMessage.writeBytes(this.document); return bytesMessage; }
public void send(byte[] bytes) throws Exception { BytesMessage bm = session.createBytesMessage(); bm.writeBytes(bytes); bm.setStringProperty("channelId", channelId); producer.send(bm); }
ProducerWorker(Session session, String workerId) throws JMSException { this.session = session; producer = session.createProducer(target); producer.setDeliveryMode(DeliveryMode.PERSISTENT); message = session.createBytesMessage(); message.setStringProperty("workerId", workerId); message.writeBytes(data); }
/** * Map the given object to a {@link javax.jms.BytesMessage}. * @param object the object to be mapped * @param session current JMS session * @param objectMapper the mapper to use * @return the resulting message * @throws javax.jms.JMSException if thrown by JMS methods * @throws java.io.IOException in case of I/O errors * @see javax.jms.Session#createBytesMessage * @see org.springframework.oxm.Marshaller#marshal(Object, javax.xml.transform.Result) */ protected BytesMessage mapToBytesMessage(Object object, Session session, ObjectMapper objectMapper) throws JMSException, IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); OutputStreamWriter writer = new OutputStreamWriter(bos, this.encoding); objectMapper.writeValue(writer, object); BytesMessage message = session.createBytesMessage(); message.writeBytes(bos.toByteArray()); if (this.encodingPropertyName != null) { message.setStringProperty(this.encodingPropertyName, this.encoding); } return message; }
/** * Map the given object to a {@link BytesMessage}. * @param object the object to be mapped * @param session current JMS session * @param objectWriter the writer to use * @return the resulting message * @throws JMSException if thrown by JMS methods * @throws IOException in case of I/O errors * @since 4.3 * @see Session#createBytesMessage */ protected BytesMessage mapToBytesMessage(Object object, Session session, ObjectWriter objectWriter) throws JMSException, IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); OutputStreamWriter writer = new OutputStreamWriter(bos, this.encoding); objectWriter.writeValue(writer, object); BytesMessage message = session.createBytesMessage(); message.writeBytes(bos.toByteArray()); if (this.encodingPropertyName != null) { message.setStringProperty(this.encodingPropertyName, this.encoding); } return message; }
/** * Map the given object to a {@link BytesMessage}. * @param object the object to be mapped * @param session current JMS session * @param objectMapper the mapper to use * @return the resulting message * @throws JMSException if thrown by JMS methods * @throws IOException in case of I/O errors * @see Session#createBytesMessage * @see Marshaller#marshal(Object, Result) */ protected BytesMessage mapToBytesMessage(Object object, Session session, ObjectMapper objectMapper) throws JMSException, IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); OutputStreamWriter writer = new OutputStreamWriter(bos, this.encoding); objectMapper.writeValue(writer, object); BytesMessage message = session.createBytesMessage(); message.writeBytes(bos.toByteArray()); if (this.encodingPropertyName != null) { message.setStringProperty(this.encodingPropertyName, this.encoding); } return message; }
private void writeWindowId(String appId, int operatorId, long windowId) throws JMSException { BytesMessage message = getBase().getSession().createBytesMessage(); message.setStringProperty(APP_OPERATOR_ID, appId + "_" + operatorId); message.writeLong(windowId); producer.send(message); logger.debug("Message with windowId {} sent", windowId); }
public void reset(Long id) { synchronized (queue) { queue.clear(); } if (publishUpdateEvents) { if (connection == null || session == null) { try { connect(); } catch (BocaException be) { log.error(this, be); return; } } if (connection != null && session != null) { try { BytesMessage bytesMessage = session.createBytesMessage(); bytesMessage.setStringProperty(BocaSerializationConstants.operation, BocaSerializationConstants.reset); publishBrokerMessage(bytesMessage); } catch (JMSException jmsex) { } } } } });
private void postOneWayBook(Session session, Destination destination) throws Exception { MessageProducer producer = session.createProducer(destination); byte[] payload = writeBook(new Book("JMS OneWay", 125L)); BytesMessage message = session.createBytesMessage(); message.writeBytes(payload); message.setStringProperty("Content-Type", "application/xml"); message.setStringProperty(org.apache.cxf.message.Message.REQUEST_URI, "/bookstore/oneway"); message.setStringProperty(org.apache.cxf.message.Message.HTTP_REQUEST_METHOD, "PUT"); producer.send(message); producer.close(); }
private void postBook(Session session, Destination destination, Destination replyTo) throws Exception { MessageProducer producer = session.createProducer(destination); byte[] payload = writeBook(new Book("JMS", 3L)); BytesMessage message = session.createBytesMessage(); message.writeBytes(payload); message.setJMSReplyTo(replyTo); // or, if oneway, // message.setStringProperty("OnewayRequest", "true"); // we could've set this header in JMSDestination if no replyTo were set // but in CXF one could also provide the replyTo in the configuration // so it is just simpler to set this header if needed to avoid some // complex logic on the server side // all these properties are optional // CXF JAXRS and JMS Transport will default to // Content-Type : text/xml // Accept : */* // POST // Message.REQUEST_URI : "/" message.setStringProperty("Content-Type", "application/xml"); message.setStringProperty("Accept", "text/xml"); message.setStringProperty(org.apache.cxf.message.Message.REQUEST_URI, "/bookstore/books"); message.setStringProperty(org.apache.cxf.message.Message.HTTP_REQUEST_METHOD, "POST"); message.setStringProperty("custom.protocol.header", "custom.value"); producer.send(message); producer.close(); }
@Test public void shouldSetMessageTraceIdInProperty() throws JMSException { // when messageSender.send(SOME_MESSAGE); // then verify(messageMock).setStringProperty("TraceId", "traceId"); } }
public void sendJmsMessage(byte[] data, String propertyName, String propertyValue, Destination destination) throws Exception { MessageProducer producer = session.createProducer(destination); BytesMessage message = session.createBytesMessage(); message.setStringProperty(propertyName, propertyValue); message.writeBytes(data); producer.send(message); }
@Test public void shouldSetMessageIdInProperty() throws JMSException { // when messageSender.send(SOME_MESSAGE); // then verify(messageMock).setStringProperty(MESSAGE_ID.getCamelCaseName(), "id"); }
protected void sendMessagesToBroker(Connection connection, int count, AtomicInteger sequence) throws Exception { Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = session.createQueue(getQueueName()); MessageProducer producer = session.createProducer(queue); byte[] buffer = new byte[MESSAGE_SIZE]; for (count = 0; count < MESSAGE_SIZE; count++) { String s = String.valueOf(count % 10); Character c = s.charAt(0); int value = c.charValue(); buffer[count] = (byte) value; } LOG.debug("Sending {} messages to destination: {}", MESSAGE_COUNT, queue); for (int i = 1; i <= MESSAGE_COUNT; i++) { BytesMessage message = session.createBytesMessage(); message.setJMSDeliveryMode(DeliveryMode.PERSISTENT); message.setStringProperty("JMSXGroupID", JMSX_GROUP_ID); message.setIntProperty("JMSXGroupSeq", sequence.incrementAndGet()); message.writeBytes(buffer); producer.send(message); } session.close(); } }
@Override protected void sendMessage(Message msg, final CompletableFuture<MessageSendingResult> resultFuture) { try { BytesMessage message = jmsContext.createBytesMessage(); message.writeBytes(msg.getData()); message.setStringProperty(TOPIC_NAME.getCamelCaseName(), msg.getTopic()); message.setStringProperty(MESSAGE_ID.getCamelCaseName(), msg.getId()); metadataAppender.append(message, msg); CompletionListener asyncListener = new CompletionListener() { @Override public void onCompletion(javax.jms.Message message) { resultFuture.complete(succeededResult()); } @Override public void onException(javax.jms.Message message, Exception exception) { logger.warn(String.format("Exception while sending message to topic %s", topicName), exception); resultFuture.complete(failedResult(exception)); } }; jmsContext.createProducer() .setAsync(asyncListener) .send(jmsContext.createTopic(topicName), message); } catch (JMSException | JMSRuntimeException e) { resultFuture.complete(failedResult(e)); } }
@Test public void testSendContentType() throws Exception { conn.connect(defUser, defPass); subscribe(conn, "sub1", Stomp.Headers.Subscribe.AckModeValues.AUTO); MessageProducer producer = session.createProducer(queue); BytesMessage message = session.createBytesMessage(); message.setStringProperty(org.apache.activemq.artemis.api.core.Message.HDR_CONTENT_TYPE.toString(), "text/plain"); message.writeBytes("Hello World".getBytes(StandardCharsets.UTF_8)); producer.send(message); ClientStompFrame frame = conn.receiveFrame(); Assert.assertNotNull(frame); Assert.assertEquals("text/plain", frame.getHeader(Stomp.Headers.CONTENT_TYPE)); conn.disconnect(); }
@Test public void bytesMessage() throws Exception { context = cf.createContext(); try { JMSProducer producer = context.createProducer(); BytesMessage bMsg = context.createBytesMessage(); bMsg.setStringProperty("COM_SUN_JMS_TESTNAME", "sendAndRecvMsgOfEachTypeCLTest"); bMsg.writeByte((byte) 1); bMsg.writeInt(22); CountDownLatch latch = new CountDownLatch(1); SimpleCompletionListener listener = new SimpleCompletionListener(latch); producer.setAsync(listener); producer.send(queue1, bMsg); assertTrue(latch.await(5, TimeUnit.SECONDS)); assertEquals(listener.message.readByte(), (byte) 1); assertEquals(listener.message.readInt(), 22); } finally { context.close(); } }
@Test public void testSubscribeWithMessageSentWithProperties() throws Exception { conn.connect(defUser, defPass); subscribe(conn, "sub1", Stomp.Headers.Subscribe.AckModeValues.AUTO); MessageProducer producer = session.createProducer(queue); BytesMessage message = session.createBytesMessage(); message.setStringProperty("S", "value"); message.setBooleanProperty("n", false); message.setByteProperty("byte", (byte) 9); message.setDoubleProperty("d", 2.0); message.setFloatProperty("f", (float) 6.0); message.setIntProperty("i", 10); message.setLongProperty("l", 121); message.setShortProperty("s", (short) 12); message.writeBytes("Hello World".getBytes(StandardCharsets.UTF_8)); producer.send(message); ClientStompFrame frame = conn.receiveFrame(); Assert.assertNotNull(frame); Assert.assertTrue(frame.getHeader("S") != null); Assert.assertTrue(frame.getHeader("n") != null); Assert.assertTrue(frame.getHeader("byte") != null); Assert.assertTrue(frame.getHeader("d") != null); Assert.assertTrue(frame.getHeader("f") != null); Assert.assertTrue(frame.getHeader("i") != null); Assert.assertTrue(frame.getHeader("l") != null); Assert.assertTrue(frame.getHeader("s") != null); Assert.assertEquals("Hello World", frame.getBody()); conn.disconnect(); }