javax.jms.StreamMessage Java Examples

The following examples show how to use javax.jms.StreamMessage. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConsumeJMSManualTest.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testStreamMessage() throws Exception {
    MessageCreator messageCreator = session -> {
        StreamMessage message = session.createStreamMessage();

        message.writeBoolean(true);
        message.writeByte(Integer.valueOf(1).byteValue());
        message.writeBytes(new byte[] {2, 3, 4});
        message.writeShort((short)32);
        message.writeInt(64);
        message.writeLong(128L);
        message.writeFloat(1.25F);
        message.writeDouble(100.867);
        message.writeChar('c');
        message.writeString("someString");
        message.writeObject("stringAsObject");

        return message;
    };

    send(messageCreator);
}
 
Example #2
Source File: JmsFactory.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
private static byte[] getMessageBytes(StreamMessage message) throws JMSException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();

    byte[] byteBuffer = new byte[4096];
    int byteCount;
    while ((byteCount = message.readBytes(byteBuffer)) != -1) {
        baos.write(byteBuffer, 0, byteCount);
    }

    try {
        baos.close();
    } catch (final IOException ioe) {
    }

    return baos.toByteArray();
}
 
Example #3
Source File: JMSUtils.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * Method to infer the JMS message type.
 *
 * @param msg the message to be inferred
 * @return the type of the JMS message
 */
public static String inferJMSMessageType(Message msg) {
    if (isTextMessage(msg)) {
        return TextMessage.class.getName();
    } else if (isBytesMessage(msg)) {
        return BytesMessage.class.getName();
    } else if (isObjectMessage(msg)) {
        return ObjectMessage.class.getName();
    } else if (isStreamMessage(msg)) {
        return StreamMessage.class.getName();
    } else if (isMapMessage(msg)) {
        return MapMessage.class.getName();
    } else {
        return null;
    }
}
 
Example #4
Source File: FatalJmsExceptionMessageCreator.java    From jadira with Apache License 2.0 6 votes vote down vote up
private static byte[] extractByteArrayFromMessage(StreamMessage message) throws JMSException {

        ByteArrayOutputStream oStream = new ByteArrayOutputStream(BUFFER_CAPACITY_BYTES);

        byte[] buffer = new byte[BUFFER_CAPACITY_BYTES];

        int bufferCount = -1;

        while ((bufferCount = message.readBytes(buffer)) >= 0) {
            oStream.write(buffer, 0, bufferCount);
            if (bufferCount < BUFFER_CAPACITY_BYTES) {
                break;
            }
        }

        return oStream.toByteArray();
    }
 
Example #5
Source File: JMSObjectInputOperator.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
/**
 * This implementation converts a TextMessage back to a String, a
 * ByteMessage back to a byte array, a MapMessage back to a Map,
 * and an ObjectMessage back to a Serializable object. Returns
 * the plain Message object in case of an unknown message type.
 *
 * @return payload
 * @throws javax.jms.JMSException
 */
@Override
public Object convert(Message message) throws JMSException
{
  if (message instanceof TextMessage) {
    return ((TextMessage)message).getText();
  } else if (message instanceof StreamMessage) {
    return ((StreamMessage)message).readString();
  } else if (message instanceof BytesMessage) {
    return extractByteArrayFromMessage((BytesMessage)message);
  } else if (message instanceof MapMessage) {
    return extractMapFromMessage((MapMessage)message);
  } else if (message instanceof ObjectMessage) {
    return extractSerializableFromMessage((ObjectMessage)message);
  } else {
    return message;
  }
}
 
Example #6
Source File: JmsFactory.java    From nifi with Apache License 2.0 6 votes vote down vote up
private static byte[] getMessageBytes(StreamMessage message) throws JMSException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();

    byte[] byteBuffer = new byte[4096];
    int byteCount;
    while ((byteCount = message.readBytes(byteBuffer)) != -1) {
        baos.write(byteBuffer, 0, byteCount);
    }

    try {
        baos.close();
    } catch (final IOException ioe) {
    }

    return baos.toByteArray();
}
 
Example #7
Source File: MessageSender.java    From fixflow with Apache License 2.0 6 votes vote down vote up
public void process(Session session,Destination engine) throws Exception {
	        MessageProducer producer = session.createProducer(engine);
	        //通知客户端开始接受文件
	        StreamMessage message = session.createStreamMessage();
	        
	        //开始发送文件
	        byte[] content = new byte[4096];
	        BufferedInputStream bins = new BufferedInputStream(ins);
	        while (bins.read(content) > 0) {
	            message = session.createStreamMessage();
//	            message.clearBody();
	            message.writeBytes(content);
	            producer.send(message);
	        }
	        bins.close();
	        ins.close();
		}
 
Example #8
Source File: StreamMessageTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Override
protected void assertEquivalent(final Message m, final int mode, final boolean redelivery) throws JMSException {
   super.assertEquivalent(m, mode, redelivery);

   StreamMessage sm = (StreamMessage) m;

   sm.reset();

   ProxyAssertSupport.assertEquals(true, sm.readBoolean());
   ProxyAssertSupport.assertEquals((byte) 3, sm.readByte());
   byte[] bytes = new byte[3];
   sm.readBytes(bytes);
   ProxyAssertSupport.assertEquals((byte) 4, bytes[0]);
   ProxyAssertSupport.assertEquals((byte) 5, bytes[1]);
   ProxyAssertSupport.assertEquals((byte) 6, bytes[2]);
   ProxyAssertSupport.assertEquals(-1, sm.readBytes(bytes));
   ProxyAssertSupport.assertEquals((char) 7, sm.readChar());
   ProxyAssertSupport.assertEquals(new Double(8.0), new Double(sm.readDouble()));
   ProxyAssertSupport.assertEquals(new Float(9.0), new Float(sm.readFloat()));
   ProxyAssertSupport.assertEquals(10, sm.readInt());
   ProxyAssertSupport.assertEquals(11L, sm.readLong());
   ProxyAssertSupport.assertEquals("this is an object", sm.readObject());
   ProxyAssertSupport.assertEquals((short) 12, sm.readShort());
   ProxyAssertSupport.assertEquals("this is a String", sm.readString());
}
 
Example #9
Source File: StreamMessageTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Override
protected void prepareMessage(final Message m) throws JMSException {
   super.prepareMessage(m);

   StreamMessage sm = (StreamMessage) m;

   sm.writeBoolean(true);
   sm.writeByte((byte) 3);
   sm.writeBytes(new byte[]{(byte) 4, (byte) 5, (byte) 6});
   sm.writeChar((char) 7);
   sm.writeDouble(8.0);
   sm.writeFloat(9.0f);
   sm.writeInt(10);
   sm.writeLong(11L);
   sm.writeObject("this is an object");
   sm.writeShort((short) 12);
   sm.writeString("this is a String");
}
 
Example #10
Source File: JMSMessageConverter.java    From cxf with Apache License 2.0 6 votes vote down vote up
public Object fromMessage(Message message) throws JMSException {
    if (message instanceof TextMessage) {
        return ((TextMessage)message).getText();
    } else if (message instanceof BytesMessage) {
        BytesMessage message1 = (BytesMessage)message;
        byte[] bytes = new byte[(int)message1.getBodyLength()];
        message1.readBytes(bytes);
        return bytes;
    } else if (message instanceof ObjectMessage) {
        return ((ObjectMessage)message).getObject();
    } else if (message instanceof StreamMessage) {
        StreamMessage streamMessage = (StreamMessage)message;
        return streamMessage.readObject();
    } else {
        return new byte[]{};
    }
}
 
Example #11
Source File: ForeignStreamMessageTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Override
protected void assertEquivalent(final Message m, final int mode, final boolean redelivery) throws JMSException {
   super.assertEquivalent(m, mode, redelivery);

   StreamMessage sm = (StreamMessage) m;

   ProxyAssertSupport.assertTrue(sm.readBoolean());

   byte[] bytes = new byte[5];
   sm.readBytes(bytes);
   String s = new String(bytes);
   ProxyAssertSupport.assertEquals("jboss", s);
   ProxyAssertSupport.assertEquals(-1, sm.readBytes(bytes));

   ProxyAssertSupport.assertEquals(sm.readChar(), 'c');
   ProxyAssertSupport.assertEquals(sm.readDouble(), 1.0D, 0.0D);
   ProxyAssertSupport.assertEquals(sm.readFloat(), 2.0F, 0.0F);
   ProxyAssertSupport.assertEquals(sm.readInt(), 3);
   ProxyAssertSupport.assertEquals(sm.readLong(), 4L);
   ProxyAssertSupport.assertEquals(sm.readObject(), "object");
   ProxyAssertSupport.assertEquals(sm.readShort(), (short) 5);
   ProxyAssertSupport.assertEquals(sm.readString(), "stringvalue");
}
 
Example #12
Source File: CompressionOverNetworkTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testStreamMessageCompression() throws Exception {

   MessageConsumer consumer1 = remoteSession.createConsumer(included);
   MessageProducer producer = localSession.createProducer(included);
   producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

   waitForConsumerRegistration(localBroker, 1, included);

   StreamMessage test = localSession.createStreamMessage();

   for (int i = 0; i < 100; ++i) {
      test.writeString("test string: " + i);
   }

   producer.send(test);
   Message msg = consumer1.receive(RECEIVE_TIMEOUT_MILLS);
   assertNotNull(msg);
   ActiveMQStreamMessage message = (ActiveMQStreamMessage) msg;
   assertTrue(message.isCompressed());

   for (int i = 0; i < 100; ++i) {
      assertEquals("test string: " + i, message.readString());
   }
}
 
Example #13
Source File: MessageCompressionTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
private void sendTestStreamMessage(ActiveMQConnectionFactory factory, String message) throws JMSException {
   ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection();
   Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   MessageProducer producer = session.createProducer(queue);
   StreamMessage streamMessage = session.createStreamMessage();

   streamMessage.writeBoolean(true);
   streamMessage.writeByte((byte) 10);
   streamMessage.writeBytes(TEXT.getBytes());
   streamMessage.writeChar('A');
   streamMessage.writeDouble(55.3D);
   streamMessage.writeFloat(79.1F);
   streamMessage.writeInt(37);
   streamMessage.writeLong(56652L);
   streamMessage.writeObject(new String("VVVV"));
   streamMessage.writeShort((short) 333);
   streamMessage.writeString(TEXT);

   producer.send(streamMessage);
   connection.close();
}
 
Example #14
Source File: MessageTypeTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
/**
 * Send a <code>StreamMessage</code> with 2 Java primitives in its body (a <code>
 * String</code> and a <code>double</code>).
 * <br />
 * Receive it and test that the values of the primitives of the body are correct
 */
@Test
public void testStreamMessage_2() {
   try {
      StreamMessage message = senderSession.createStreamMessage();
      message.writeString("pi");
      message.writeDouble(3.14159);
      sender.send(message);

      Message m = receiver.receive(TestConfig.TIMEOUT);
      Assert.assertTrue("The message should be an instance of StreamMessage.\n", m instanceof StreamMessage);
      StreamMessage msg = (StreamMessage) m;
      Assert.assertEquals("pi", msg.readString());
      Assert.assertEquals(3.14159, msg.readDouble(), 0);
   } catch (JMSException e) {
      fail(e);
   }
}
 
Example #15
Source File: TestGetJMSQueue.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendStreamToQueue() throws Exception {
    PutJMS putJms = new PutJMS();
    TestRunner putRunner = TestRunners.newTestRunner(putJms);
    putRunner.setProperty(JmsProperties.JMS_PROVIDER, JmsProperties.ACTIVEMQ_PROVIDER);
    putRunner.setProperty(JmsProperties.URL, "vm://localhost?broker.persistent=false");
    putRunner.setProperty(JmsProperties.DESTINATION_TYPE, JmsProperties.DESTINATION_TYPE_QUEUE);
    putRunner.setProperty(JmsProperties.DESTINATION_NAME, "queue.testing");
    putRunner.setProperty(JmsProperties.ACKNOWLEDGEMENT_MODE, JmsProperties.ACK_MODE_AUTO);
    WrappedMessageProducer wrappedProducer = JmsFactory.createMessageProducer(putRunner.getProcessContext(), true);
    final Session jmsSession = wrappedProducer.getSession();
    final MessageProducer producer = wrappedProducer.getProducer();

    final StreamMessage message = jmsSession.createStreamMessage();
    message.writeBytes("Hello Stream".getBytes());

    producer.send(message);
    jmsSession.commit();

    GetJMSQueue getJmsQueue = new GetJMSQueue();
    TestRunner runner = TestRunners.newTestRunner(getJmsQueue);
    runner.setProperty(JmsProperties.JMS_PROVIDER, JmsProperties.ACTIVEMQ_PROVIDER);
    runner.setProperty(JmsProperties.URL, "vm://localhost?broker.persistent=false");
    runner.setProperty(JmsProperties.DESTINATION_NAME, "queue.testing");
    runner.setProperty(JmsProperties.ACKNOWLEDGEMENT_MODE, JmsProperties.ACK_MODE_AUTO);

    runner.run();

    List<MockFlowFile> flowFiles = runner
            .getFlowFilesForRelationship(new Relationship.Builder().name("success").build());

    assertTrue(flowFiles.size() == 1);
    MockFlowFile successFlowFile = flowFiles.get(0);
    successFlowFile.assertContentEquals("Hello Stream");
    successFlowFile.assertAttributeEquals("jms.JMSDestination", "queue.testing");

    producer.close();
    jmsSession.close();
}
 
Example #16
Source File: ActiveMQRAStreamMessage.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
/**
 * Write
 *
 * @param value The value
 * @throws JMSException Thrown if an error occurs
 */
@Override
public void writeBoolean(final boolean value) throws JMSException {
   if (ActiveMQRALogger.LOGGER.isTraceEnabled()) {
      ActiveMQRALogger.LOGGER.trace("writeBoolean(" + value + ")");
   }

   ((StreamMessage) message).writeBoolean(value);
}
 
Example #17
Source File: ActiveMQRAStreamMessage.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
/**
 * Reset
 *
 * @throws JMSException Thrown if an error occurs
 */
@Override
public void reset() throws JMSException {
   if (ActiveMQRALogger.LOGGER.isTraceEnabled()) {
      ActiveMQRALogger.LOGGER.trace("reset()");
   }

   ((StreamMessage) message).reset();
}
 
Example #18
Source File: ActiveMQRAStreamMessage.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
/**
 * Read
 *
 * @return The value
 * @throws JMSException Thrown if an error occurs
 */
@Override
public char readChar() throws JMSException {
   if (ActiveMQRALogger.LOGGER.isTraceEnabled()) {
      ActiveMQRALogger.LOGGER.trace("readChar()");
   }

   return ((StreamMessage) message).readChar();
}
 
Example #19
Source File: ActiveMQRAStreamMessage.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
/**
 * Write
 *
 * @param value The value
 * @throws JMSException Thrown if an error occurs
 */
@Override
public void writeObject(final Object value) throws JMSException {
   if (ActiveMQRALogger.LOGGER.isTraceEnabled()) {
      ActiveMQRALogger.LOGGER.trace("writeObject(" + value + ")");
   }

   ((StreamMessage) message).writeObject(value);
}
 
Example #20
Source File: ActiveMQRAStreamMessage.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
/**
 * Read
 *
 * @return The value
 * @throws JMSException Thrown if an error occurs
 */
@Override
public byte readByte() throws JMSException {
   if (ActiveMQRALogger.LOGGER.isTraceEnabled()) {
      ActiveMQRALogger.LOGGER.trace("readByte()");
   }

   return ((StreamMessage) message).readByte();
}
 
Example #21
Source File: ActiveMQRAStreamMessage.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new wrapper
 *
 * @param message the message
 * @param session the session
 */
public ActiveMQRAStreamMessage(final StreamMessage message, final ActiveMQRASession session) {
   super(message, session);

   if (ActiveMQRALogger.LOGGER.isTraceEnabled()) {
      ActiveMQRALogger.LOGGER.trace("constructor(" + message + ", " + session + ")");
   }
}
 
Example #22
Source File: ActiveMQRASession.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
/**
 * Create a stream message
 *
 * @return The message
 * @throws JMSException Thrown if an error occurs
 */
@Override
public StreamMessage createStreamMessage() throws JMSException {
   Session session = getSessionInternal();

   if (ActiveMQRALogger.LOGGER.isTraceEnabled()) {
      ActiveMQRALogger.LOGGER.trace("createStreamMessage" + session);
   }

   return session.createStreamMessage();
}
 
Example #23
Source File: ActiveMQRAStreamMessage.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
/**
 * Write
 *
 * @param value The value
 * @throws JMSException Thrown if an error occurs
 */
@Override
public void writeShort(final short value) throws JMSException {
   if (ActiveMQRALogger.LOGGER.isTraceEnabled()) {
      ActiveMQRALogger.LOGGER.trace("writeShort(" + value + ")");
   }

   ((StreamMessage) message).writeShort(value);
}
 
Example #24
Source File: ActiveMQRAStreamMessage.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
/**
 * Write
 *
 * @param value The value
 * @throws JMSException Thrown if an error occurs
 */
@Override
public void writeString(final String value) throws JMSException {
   if (ActiveMQRALogger.LOGGER.isTraceEnabled()) {
      ActiveMQRALogger.LOGGER.trace("writeString(" + value + ")");
   }

   ((StreamMessage) message).writeString(value);
}
 
Example #25
Source File: EmbeddedJMSResource.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
public StreamMessage createStreamMessage() {
   checkSession();
   try {
      return session.createStreamMessage();
   } catch (JMSException jmsEx) {
      throw new EmbeddedJMSResourceException("Failed to create StreamMessage", jmsEx);
   }
}
 
Example #26
Source File: MessageBodyTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testSMBodyReadable() throws Exception {
   byte bValue = 123;
   StreamMessage sm = queueProducerSession.createStreamMessage();
   sm.writeByte(bValue);
   sm.setStringProperty("COM_SUN_JMS_TESTNAME", "xMessageEOFExceptionQTestforStreamMessage");
   queueProducer.send(sm);

   StreamMessage received = (StreamMessage) queueConsumer.receive(3000);
   received.readByte();
}
 
Example #27
Source File: ActiveMQStreamMessage.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
public ActiveMQStreamMessage(final StreamMessage foreign, final ClientSession session) throws JMSException {
   super(foreign, ActiveMQStreamMessage.TYPE, session);

   foreign.reset();

   try {
      while (true) {
         Object obj = foreign.readObject();
         writeObject(obj);
      }
   } catch (MessageEOFException e) {
      // Ignore
   }
}
 
Example #28
Source File: JMSQueueMessageProducer.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * Method to send a StreamMessage.
 *
 * @param payload content of the StreamMessage to be sent
 * @throws JMSException if an error occurs sending the BytesMessage
 */
public void sendStreamMessage(byte[] payload) throws JMSException {
    checkIfConnected();
    StreamMessage streamMessage = session.createStreamMessage();
    streamMessage.writeBytes(payload);
    producer.send(streamMessage);
}
 
Example #29
Source File: JMSContextImpl.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
public StreamMessage createStreamMessage() {
    try {
        return wrap(session().createStreamMessage());
    } catch (final JMSException e) {
        throw toRuntimeException(e);
    }
}
 
Example #30
Source File: ActiveMQRAStreamMessage.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
/**
 * Write
 *
 * @param value The value
 * @throws JMSException Thrown if an error occurs
 */
@Override
public void writeLong(final long value) throws JMSException {
   if (ActiveMQRALogger.LOGGER.isTraceEnabled()) {
      ActiveMQRALogger.LOGGER.trace("writeLong(" + value + ")");
   }

   ((StreamMessage) message).writeLong(value);
}