當前位置: 首頁>>代碼示例>>Java>>正文


Java BytesMessage.reset方法代碼示例

本文整理匯總了Java中javax.jms.BytesMessage.reset方法的典型用法代碼示例。如果您正苦於以下問題:Java BytesMessage.reset方法的具體用法?Java BytesMessage.reset怎麽用?Java BytesMessage.reset使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.jms.BytesMessage的用法示例。


在下文中一共展示了BytesMessage.reset方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: prepareMessage

import javax.jms.BytesMessage; //導入方法依賴的package包/類
@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();
}
 
開發者ID:apache,項目名稱:activemq-artemis,代碼行數:19,代碼來源:BytesMessageTest.java

示例2: testCopyOnForeignBytesMessage

import javax.jms.BytesMessage; //導入方法依賴的package包/類
@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);
}
 
開發者ID:apache,項目名稱:activemq-artemis,代碼行數:18,代碼來源:MessageHeaderTest.java

示例3: writeBinaryContentToFile

import javax.jms.BytesMessage; //導入方法依賴的package包/類
/**
     * Write message binary body to provided file or default one in temp directory.
     *
     * @param filePath file to write data to
     * @param message  to be read and written to provided file
     */
    public static void writeBinaryContentToFile(String filePath, Message message, int msgCounter) {
        byte[] readByteArray;
        try {
            File writeBinaryFile;
            if (filePath == null || filePath.equals("")) {
                writeBinaryFile = File.createTempFile("recv_msg_", Long.toString(System.currentTimeMillis()));
            } else {
                writeBinaryFile = new File(filePath + "_" + msgCounter);
            }

            LOG.debug("Write binary content to file '" + writeBinaryFile.getPath() + "'.");
            if (message instanceof BytesMessage) {
                BytesMessage bm = (BytesMessage) message;
                readByteArray = new byte[(int) bm.getBodyLength()];
                bm.reset(); // added to be able to read message content
                bm.readBytes(readByteArray);
                try (FileOutputStream fos = new FileOutputStream(writeBinaryFile)) {
                    fos.write(readByteArray);
                    fos.close();
                }

            } else if (message instanceof StreamMessage) {
                LOG.debug("Writing StreamMessage to");
                StreamMessage sm = (StreamMessage) message;
//        sm.reset(); TODO haven't tested this one
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(baos);
                oos.writeObject(sm.readObject());
                oos.close();
            }

        } catch (JMSException e) {
            e.printStackTrace();
        } catch (IOException e1) {
            LOG.error("Error while writing to file '" + filePath + "'.");
            e1.printStackTrace();
        }
    }
 
開發者ID:rh-messaging,項目名稱:cli-java,代碼行數:45,代碼來源:Utils.java

示例4: testBodyConversion

import javax.jms.BytesMessage; //導入方法依賴的package包/類
@Test
public void testBodyConversion() throws Throwable {
   try (
      Connection conn = cf.createConnection();
   ) {
      Session sess = conn.createSession();
      MessageProducer producer = sess.createProducer(queue);

      MessageConsumer cons = sess.createConsumer(queue);
      conn.start();

      BytesMessage bytesMessage = sess.createBytesMessage();
      BytesMessage bytesMessage2 = sess.createBytesMessage();
      bytesMessage2.writeInt(42);
      bytesMessage2.reset();

      producer.send(bytesMessage);
      Message msg = cons.receiveNoWait();

      producer.send(bytesMessage2);
      Message msg2 = cons.receiveNoWait();

      assertNotNull(msg);
      assertNotNull(msg2);

      // message body is empty. getBody parameter may be set to any type
      Assert.assertNull(msg.getBody(java.lang.Object.class));
      Assert.assertNull(msg.getBody(byte[].class));
      Assert.assertNull(msg.getBody(String.class));

      // message body is not empty. getBody parameter must be set to byte[].class (or java.lang.Object.class)
      Assert.assertNotNull(msg2.getBody(byte[].class));
      Assert.assertNotNull(msg2.getBody(java.lang.Object.class));
   }
}
 
開發者ID:apache,項目名稱:activemq-artemis,代碼行數:36,代碼來源:BodyTest.java

示例5: ActiveMQBytesMessage

import javax.jms.BytesMessage; //導入方法依賴的package包/類
/**
 * 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);
   }
}
 
開發者ID:apache,項目名稱:activemq-artemis,代碼行數:16,代碼來源:ActiveMQBytesMessage.java

示例6: castTo

import javax.jms.BytesMessage; //導入方法依賴的package包/類
public  static  <T> T castTo(Class<T> c, Message message) throws JMSException {
    if (message == null) {
        throw new MessageFormatRuntimeException("empty body");
    }

    if (!isBodyAssignableTo(message, c)) {
        throw new MessageFormatRuntimeException("the message body cannot be assigned to the specified type "
                + c
                + " and the Message type "
                + StreamMessage.class
                + " cannot be used");
    }

    if (message instanceof ObjectMessage) {
        return c.cast(((ObjectMessage) message).getObject());
    } else if (message instanceof MapMessage) {
        return c.cast(resolveMapMessage((MapMessage) message));
    } else if (message instanceof BytesMessage) {
        BytesMessage bytesMessage = (BytesMessage) message;
        bytesMessage.reset();
        try {
            byte[] bytes = new byte[(int) bytesMessage.getBodyLength()];
            for (int i = 0; i < bytes.length; i++) {
                bytes[i] = bytesMessage.readByte();
            }
            return c.cast(bytes);
        } finally {
            bytesMessage.reset();
        }
    } else if (message instanceof TextMessage) {
        return c.cast(((TextMessage) message).getText());
    } else {
        throw new MessageFormatRuntimeException("internal error. unknown message type "
                + message.getClass() + " or empty body");
    }
}
 
開發者ID:Tibor17,項目名稱:javaee-samples,代碼行數:37,代碼來源:Util.java

示例7: traceMessage

import javax.jms.BytesMessage; //導入方法依賴的package包/類
/**
 * Trace message body and all its JMS properties
 * 
 * @param msg
 * @throws JMSException
 */
protected final void traceMessage(final Message msg) throws JMSException
{
	if (msg instanceof BytesMessage) {
		BytesMessage messageBody = (BytesMessage) msg;
		// message is in write mode, close & reset to start
		// of byte stream
		messageBody.reset();
		Long length = messageBody.getBodyLength();
		if (logger.isDebugEnabled())
			logger.debug("***** MESSAGE LENGTH BytesMessage is {} bytes", length);
		byte[] byteMyMessage = new byte[length.intValue()];
		messageBody.readBytes(byteMyMessage);
		if (logger.isDebugEnabled())
			logger.debug("***** TRACE MESSAGE - \n<!-- MSG START -->\n{}\n<!-- MSG END -->",
					new String(
							byteMyMessage));
		messageBody.reset();

	}
	else if (msg instanceof TextMessage)
	{
		TextMessage textMsg = (TextMessage) msg;
		String body = textMsg.getText();
		if (logger.isDebugEnabled())
			logger.debug("***** MESSAGE LENGTH TextMessage is {} bytes", body.length());
		if (logger.isDebugEnabled())
			logger.debug("***** TRACE MESSAGE - \n<!-- MSG START -->\n{}\n<!-- MSG END -->",
					body);

	}
	else if (msg instanceof ObjectMessage)
	{
		ObjectMessage oMsg = (ObjectMessage) msg;
		if (logger.isDebugEnabled())
			logger.debug("***** Binary Message ObjectMessage ******");
		if (logger.isDebugEnabled())
			logger.debug("***** TRACE MESSAGE - \n<!-- MSG START -->\n{}\n<!-- MSG END -->",
					oMsg.toString());

	}

	@SuppressWarnings("unchecked")
	Enumeration<String> en = msg.getPropertyNames();
	while (en.hasMoreElements())
	{
		String key = en.nextElement();
		Object value = msg.getObjectProperty(key);
		if (logger.isDebugEnabled())
			logger.debug("header key: " + key + " -- value: " + value.toString());

	}
	if (logger.isDebugEnabled())
	{
		logger.debug("header key: messageId-- value: " + msg.getJMSMessageID());
		logger.debug("header key: correlationId-- value: " + msg.getJMSCorrelationID());
		logger.debug("header key: expiration-- value: " + msg.getJMSExpiration());
		logger.debug("header key: priority-- value: " + msg.getJMSPriority());
		logger.debug("header key: timestamp-- value: " + msg.getJMSTimestamp());
		logger.debug("header key: type-- value: " + msg.getJMSType());
		logger.debug("header key: destination-- value: " + msg.getJMSDestination());
		logger.debug("header key: redelivered-- value: " + msg.getJMSRedelivered());
		logger.debug("header key: replyTo-- value: " + msg.getJMSReplyTo());
	}

}
 
開發者ID:Appverse,項目名稱:appverse-server,代碼行數:72,代碼來源:JMSService.java

示例8: testBytesMessageSendReceive

import javax.jms.BytesMessage; //導入方法依賴的package包/類
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++) {
      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);

   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);

      System.out.println("Received " + ByteUtil.bytesToHex(bytesReceived, 1) + " count - " + m.getIntProperty("count"));

      Assert.assertArrayEquals(bytes, bytesReceived);
   }

   long taken = (System.currentTimeMillis() - time) / 1000;
   System.out.println("taken = " + taken);
}
 
開發者ID:apache,項目名稱:activemq-artemis,代碼行數:44,代碼來源:JMSMessageTypesTest.java


注:本文中的javax.jms.BytesMessage.reset方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。