try { if (durable && destination instanceof Topic) { consumer = session.createDurableSubscriber((Topic) destination, getName()); } else { consumer = session.createConsumer(destination); Message msg = consumer.receive(receiveTimeOut); if (msg != null) { LOG.info(threadName + " Received " + (msg instanceof TextMessage ? ((TextMessage) msg).getText() : msg.getJMSMessageID())); if (bytesAsText && (msg instanceof BytesMessage)) { long length = ((BytesMessage) msg).getBodyLength(); byte[] bytes = new byte[(int) length]; ((BytesMessage) msg).readBytes(bytes); LOG.info("BytesMessage as text string: " + new String(bytes)); if (session.getTransacted()) { if (batchSize > 0 && received > 0 && received % batchSize == 0) { LOG.info(threadName + " Committing transaction: " + transactions++); LOG.info(threadName + " Consumed: " + this.getReceived() + " messages"); try { consumer.close(); } catch (JMSException e) { e.printStackTrace();
message = jmsSession.createTextMessage(""); break; final StreamMessage streamMessage = jmsSession.createStreamMessage(); streamMessage.writeBytes(messageContent); message = streamMessage; message = jmsSession.createTextMessage(new String(messageContent, UTF8)); break; default: { final BytesMessage bytesMessage = jmsSession.createBytesMessage(); bytesMessage.writeBytes(messageContent); message = bytesMessage;
/** * Foreign message constructor */ public ActiveMQBytesMessage(final BytesMessage foreign, final ClientSession session) throws JMSException { super(foreign, ActiveMQBytesMessage.TYPE, session); foreign.reset(); byte[] buffer = new byte[1024]; int n = foreign.readBytes(buffer); while (n != -1) { writeBytes(buffer, 0, n); n = foreign.readBytes(buffer); } }
/** * Extract a byte array from the given {@link BytesMessage}. * @param message the message to convert * @return the resulting byte array * @throws JMSException if thrown by JMS methods */ protected byte[] extractByteArrayFromMessage(BytesMessage message) throws JMSException { byte[] bytes = new byte[(int) message.getBodyLength()]; message.readBytes(bytes); return bytes; }
/** * Convert a BytesMessage to a Java Object with the specified type. * @param message the input message * @param targetJavaType the target type * @return the message converted to an object * @throws JMSException if thrown by JMS * @throws IOException in case of I/O errors */ protected Object convertFromBytesMessage(BytesMessage message, JavaType targetJavaType) throws JMSException, IOException { String encoding = this.encoding; if (this.encodingPropertyName != null && message.propertyExists(this.encodingPropertyName)) { encoding = message.getStringProperty(this.encodingPropertyName); } byte[] bytes = new byte[(int) message.getBodyLength()]; message.readBytes(bytes); try { String body = new String(bytes, encoding); return this.objectMapper.readValue(body, targetJavaType); } catch (UnsupportedEncodingException ex) { throw new MessageConversionException("Cannot convert bytes to String", ex); } }
/** * 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; }
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = session.createQueue(name.getMethodName()); MessageProducer producer = session.createProducer(queue); BytesMessage message = session.createBytesMessage(); message.writeBytes(payload); producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); producer.setPriority(4); producer.send(message); long endTime = System.currentTimeMillis(); startTime = System.currentTimeMillis(); MessageConsumer consumer = session.createConsumer(queue); connection.start(); Message received = consumer.receive(); assertNotNull(received); assertTrue(received instanceof BytesMessage); assertEquals(expectedSize, bytesMessage.readBytes(bytesReceived, expectedSize)); assertTrue(Arrays.equals(payload, bytesReceived)); connection.close();
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://" + OWHOST + ":" + OWPORT + "?jms.useCompression=true"); connection = factory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = session.createQueue(queueName); MessageProducer producer = session.createProducer(queue); producer.setDeliveryMode(DeliveryMode.PERSISTENT); TextMessage textMessage = session.createTextMessage(); textMessage.setText(testString); TextMessage receivedMessage = sendAndReceive(textMessage, producer, consumer); String receivedText = receivedMessage.getText(); assertEquals(testString, receivedText); BytesMessage byteMessage = session.createBytesMessage(); byte[] bytes = testString.getBytes(); byteMessage.writeBytes(bytes); long receivedBodylength = receivedByteMessage.getBodyLength(); receivedByteMessage.readBytes(receivedBytes);
@Test public void testSendReceiveLargeMessage() throws Exception { // Create 1MB Message int size = 1024 * 1024; byte[] bytes = new byte[size]; try (Connection connection = factory.createConnection()) { connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = session.createQueue(lmAddress.toString()); MessageProducer producer = session.createProducer(queue); producer.setDeliveryMode(DeliveryMode.PERSISTENT); bytes[0] = 1; BytesMessage message = session.createBytesMessage(); message.writeBytes(bytes); producer.send(message); } server.stop(); server.start(); try (Connection connection = factory.createConnection()) { connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = session.createQueue(lmAddress.toString()); MessageConsumer consumer = session.createConsumer(queue); BytesMessage m = (BytesMessage) consumer.receive(); assertNotNull(m); byte[] body = new byte[size]; m.readBytes(body); assertArrayEquals(body, bytes); } }
Session session1 = conn1.createSession(true, Session.SESSION_TRANSACTED); MessageProducer prod1 = session1.createProducer(queue); Session session2 = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer cons2 = session2.createConsumer(queue); conn2.start(); BytesMessage msg = session1.createBytesMessage(); msg.writeBytes(bytes); prod1.send(msg); BytesMessage msg2 = (BytesMessage) cons2.receive(5000); assertNotNull(msg2); msg2.acknowledge(); assertEquals("Position " + i, msg2.readByte(), bytes[j]);
@Test public void testSimpleLargeMessage2() throws Exception { conn = cf.createConnection(); Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer prod = session.createProducer(queue1); BytesMessage m = session.createBytesMessage(); m.setObjectProperty("JMS_AMQ_InputStream", ActiveMQTestBase.createFakeLargeStream(10)); prod.send(m); conn.close(); conn = cf.createConnection(); session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer cons = session.createConsumer(queue1); conn.start(); BytesMessage rm = (BytesMessage) cons.receive(10000); byte[] data = new byte[1024]; System.out.println("Message = " + rm); int numberOfBytes = rm.readBytes(data); Assert.assertEquals(10, numberOfBytes); for (int j = 0; j < numberOfBytes; j++) { Assert.assertEquals(ActiveMQTestBase.getSamplebyte(j), data[j]); } Assert.assertNotNull(rm); }
public void run() { try { ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(jmsBrokerUrl); Connection connection = connectionFactory.createConnection(); connection.start(); connection.setExceptionListener(this); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue(jmsQueueName); MessageConsumer consumer = session.createConsumer(destination); Message message = consumer.receive(1000); byte[] bytes = new byte[(int)bytesMessage.getBodyLength()]; bytesMessage.readBytes(bytes); String text = new String(bytes); LOG.info("JMS Consumer Received BytesMessage: " + text); } else if (message instanceof TextMessage){ TextMessage textMessage = (TextMessage) message; String text = textMessage.getText(); LOG.info("JMS Consumer Received TextMessage: " + text); } else { consumer.close(); session.close(); connection.close(); } catch (Exception e) {
try { connection = qpidfactory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = session.createQueue(coreQueue.toString()); MessageProducer producer = session.createProducer(queue); ArrayList list = new ArrayList(); list.add("aString"); ObjectMessage objectMessage = session.createObjectMessage(list); producer.send(objectMessage); connection.close(); connection = factory.createConnection(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); queue = session.createQueue(queueName); MessageConsumer consumer = session.createConsumer(queue); connection.start(); BytesMessage receive = (BytesMessage) consumer.receive(5000); assertNotNull(receive); byte[] bytes = new byte[(int) receive.getBodyLength()]; receive.readBytes(bytes); ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes)); list = (ArrayList) ois.readObject();
public void send(byte[] bytes) throws Exception { BytesMessage bm = session.createBytesMessage(); bm.writeBytes(bytes); bm.setStringProperty("channelId", channelId); producer.send(bm); }
@Override public Void call() throws Exception { ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://" + HOSTNAME + ":8500"); javax.jms.Connection connection = connectionFactory.createConnection(); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue("SITEWHERE.IN"); MessageProducer producer = session.createProducer(destination); for (int i = 0; i < messageCount; i++) { BytesMessage message = session.createBytesMessage(); message.writeBytes(EventsHelper.generateJsonMeasurementsMessage(DEVICE_TOKEN)); producer.send(message); } session.close(); connection.close(); return null; } }
@Test public void testManagedOutboundJMSWithContextProperty() throws Exception { String payload = "onMessageContextProperty"; Session session = _hqMixIn.createJMSSession(); try { MessageProducer producer = session.createProducer(HornetQMixIn.getJMSQueue(INPUT_TX_QUEUE)); TextMessage inMsg = session.createTextMessage(); inMsg.setText(payload); producer.send(inMsg); producer.close(); session.close(); session = _hqMixIn.createJMSSession(); MessageConsumer consumer = session.createConsumer(HornetQMixIn.getJMSQueue(OUTPUT_PROP_QUEUE)); javax.jms.Message msg = consumer.receive(3000); consumer.close(); Assert.assertNotNull(msg); Assert.assertTrue(msg instanceof BytesMessage); BytesMessage bmsg = BytesMessage.class.cast(msg); byte[] bytes = new byte[(int)bmsg.getBodyLength()]; bmsg.readBytes(bytes); Assert.assertEquals(payload + "test", new String(bytes)); } finally { session.close(); } }
Session session = null; try { session = connectionConsumerProducer.createSession(true, Session.SESSION_TRANSACTED); MessageConsumer consumer = session.createConsumer(ActiveMQDestination.createQueue("queue")); MessageProducer messageProducer = session.createProducer(ActiveMQDestination.createQueue("queue2")); messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); for (int i = 0; i < numberOfMessages; i++) { BytesMessage message = (BytesMessage) consumer.receive(5000); if (message == null) { return; message = session.createBytesMessage(); message.writeBytes(new byte[3000]); message.setStringProperty("Service", "LoadShedService"); message.setStringProperty("Action", "testAction"); messageProducer.send(message); session.commit();
private void testBytesMessageSendReceive(Connection producerConnection, Connection consumerConnection) throws Throwable { long time = System.currentTimeMillis(); Session session = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = session.createQueue(getQueueName()); MessageProducer producer = session.createProducer(queue); for (int i = 0; i < NUM_MESSAGES; i++) { System.out.println("Sending " + i); BytesMessage message = session.createBytesMessage(); message.writeBytes(bytes); message.setIntProperty("count", i); producer.send(message); Session sessionConsumer = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue consumerQueue = sessionConsumer.createQueue(getQueueName()); final MessageConsumer consumer = sessionConsumer.createConsumer(consumerQueue); BytesMessage m = (BytesMessage) consumer.receive(5000); Assert.assertNotNull("Could not receive message count=" + i + " on consumer", m); m.reset(); long size = m.getBodyLength(); byte[] bytesReceived = new byte[(int) size]; m.readBytes(bytesReceived); System.out.println("Received " + ByteUtil.bytesToHex(bytesReceived, 1) + " count - " + m.getIntProperty("count"));
public void doTestSendJMSReceiveMQTT(String destinationName) throws Exception { final MQTTClientProvider provider = getMQTTClientProvider(); initializeConnection(provider); provider.subscribe("foo/+", AT_MOST_ONCE); Connection connection = cf.createConnection(); connection.start(); Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); javax.jms.Topic topic = s.createTopic(destinationName); MessageProducer producer = s.createProducer(topic); // send retained message from JMS final byte[] bytes = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; BytesMessage bytesMessage = s.createBytesMessage(); bytesMessage.writeBytes(bytes); producer.send(bytesMessage); byte[] message = provider.receive(10000); assertNotNull("Should get retained message", message); assertArrayEquals(bytes, message); provider.disconnect(); connection.close(); }
public void doTestJmsMapping(String destinationName) throws Exception { Session session = jmsConn.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination dest = session.createQueue(destinationName); MessageConsumer consumer = session.createConsumer(dest); jmsConn.start(); javax.jms.Message message = consumer.receive(2 * 1000); assertNotNull(message); assertTrue(message instanceof BytesMessage); BytesMessage bytesMessage = (BytesMessage) message; int length = (int) bytesMessage.getBodyLength(); byte[] buffer = new byte[length]; bytesMessage.readBytes(buffer); assertEquals("hello world", new String(buffer)); jmsConn.close();