Java Code Examples for javax.jms.BytesMessage#reset()

The following examples show how to use javax.jms.BytesMessage#reset() . 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: BytesMessageTest.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);

   BytesMessage bm = (BytesMessage) m;

   bm.writeBoolean(true);
   bm.writeByte((byte) 3);
   bm.writeBytes(new byte[]{(byte) 4, (byte) 5, (byte) 6});
   bm.writeChar((char) 7);
   bm.writeDouble(8.0);
   bm.writeFloat(9.0f);
   bm.writeInt(10);
   bm.writeLong(11L);
   bm.writeShort((short) 12);
   bm.writeUTF("this is an UTF String");
   bm.reset();
}
 
Example 2
Source File: MessageHeaderTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testCopyOnForeignBytesMessage() throws JMSException {
   ClientMessage clientMessage = new ClientMessageImpl(ActiveMQTextMessage.TYPE, true, 0, System.currentTimeMillis(), (byte) 4, 1000);
   ClientSession session = new FakeSession(clientMessage);

   BytesMessage foreignBytesMessage = new SimpleJMSBytesMessage();
   for (int i = 0; i < 20; i++) {
      foreignBytesMessage.writeByte((byte) i);
   }

   ActiveMQBytesMessage copy = new ActiveMQBytesMessage(foreignBytesMessage, session);

   foreignBytesMessage.reset();
   copy.reset();

   MessageHeaderTestBase.ensureEquivalent(foreignBytesMessage, copy);
}
 
Example 3
Source File: TestJmsConsumer.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Test BytesMessage to FlowFile conversion
 *
 * @throws java.lang.Exception ex
 */
@Test
public void testMap2FlowFileBytesMessage() throws Exception {

    TestRunner runner = TestRunners.newTestRunner(GetJMSQueue.class);
    BytesMessage bytesMessage = new ActiveMQBytesMessage();

    String sourceString = "Apache NiFi is an easy to use, powerful, and reliable system to process and distribute data.!";
    byte[] payload = sourceString.getBytes("UTF-8");
    bytesMessage.writeBytes(payload);
    bytesMessage.reset();

    ProcessContext context = runner.getProcessContext();
    ProcessSession session = runner.getProcessSessionFactory().createSession();
    ProcessorInitializationContext pic = new MockProcessorInitializationContext(runner.getProcessor(), (MockProcessContext) runner.getProcessContext());

    JmsProcessingSummary summary = JmsConsumer.map2FlowFile(context, session, bytesMessage, true, pic.getLogger());

    assertEquals("BytesMessage content length should equal to FlowFile content size", payload.length, summary.getLastFlowFile().getSize());

    final byte[] buffer = new byte[payload.length];
    runner.clearTransferState();

    session.read(summary.getLastFlowFile(), new InputStreamCallback() {
        @Override
        public void process(InputStream in) throws IOException {
            StreamUtils.fillBuffer(in, buffer, false);
        }
    });

    String contentString = new String(buffer, "UTF-8");
    assertEquals("", sourceString, contentString);
}
 
Example 4
Source File: ActiveMQBytesMessage.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
   }
}
 
Example 5
Source File: TestJmsConsumer.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Test BytesMessage to FlowFile conversion
 *
 * @throws java.lang.Exception ex
 */
@Test
public void testMap2FlowFileBytesMessage() throws Exception {

    TestRunner runner = TestRunners.newTestRunner(GetJMSQueue.class);
    BytesMessage bytesMessage = new ActiveMQBytesMessage();

    String sourceString = "Apache NiFi is an easy to use, powerful, and reliable system to process and distribute data.!";
    byte[] payload = sourceString.getBytes("UTF-8");
    bytesMessage.writeBytes(payload);
    bytesMessage.reset();

    ProcessContext context = runner.getProcessContext();
    ProcessSession session = runner.getProcessSessionFactory().createSession();
    ProcessorInitializationContext pic = new MockProcessorInitializationContext(runner.getProcessor(), (MockProcessContext) runner.getProcessContext());

    JmsProcessingSummary summary = JmsConsumer.map2FlowFile(context, session, bytesMessage, true, pic.getLogger());

    assertEquals("BytesMessage content length should equal to FlowFile content size", payload.length, summary.getLastFlowFile().getSize());

    final byte[] buffer = new byte[payload.length];
    runner.clearTransferState();

    session.read(summary.getLastFlowFile(), new InputStreamCallback() {
        @Override
        public void process(InputStream in) throws IOException {
            StreamUtils.fillBuffer(in, buffer, false);
        }
    });

    String contentString = new String(buffer, "UTF-8");
    assertEquals("", sourceString, contentString);
}
 
Example 6
Source File: JMSMessageTypesTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
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());

   byte[] bytes = new byte[0xf + 1];
   for (int i = 0; i <= 0xf; i++) {
      bytes[i] = (byte) i;
   }

   MessageProducer producer = session.createProducer(queue);
   for (int i = 0; i < NUM_MESSAGES; i++) {
      instanceLog.debug("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);

   for (int i = 0; i < NUM_MESSAGES; i++) {
      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);

      instanceLog.debug("Received " + ByteUtil.bytesToHex(bytesReceived, 1) + " count - " + m.getIntProperty("count"));

      Assert.assertArrayEquals(bytes, bytesReceived);
   }

   long taken = (System.currentTimeMillis() - time) / 1000;
   instanceLog.debug("taken = " + taken);
}