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


Java BytesMessage.readBytes方法代碼示例

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


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

示例1: onMessage

import javax.jms.BytesMessage; //導入方法依賴的package包/類
@Override
public void onMessage(Message message) {

  try {

    if (message instanceof BytesMessage) {

      BytesMessage bytesMessage = (BytesMessage) message;
      byte[] data = new byte[(int) bytesMessage.getBodyLength()];
      bytesMessage.readBytes(data);
      LOG.info("Message received {}", new String(data));

    } else if (message instanceof TextMessage) {

      TextMessage textMessage = (TextMessage) message;
      String text = textMessage.getText();
      LOG.info("Message received {}", text);
    }

  } catch (JMSException jmsEx) {
    jmsEx.printStackTrace();
  }
}
 
開發者ID:ppatierno,項目名稱:amqp-kafka-demo,代碼行數:24,代碼來源:Receiver.java

示例2: getBytes

import javax.jms.BytesMessage; //導入方法依賴的package包/類
/**
 * Read the the payload of the message and return it in a byte array.
 * 
 * @param msg
 * @return
 */
private byte[] getBytes(final Message msg) {
    byte[] data = null;
    try {
        if (msg instanceof BytesMessage) {
            final BytesMessage tmp = (BytesMessage) msg;
            int len;
            len = (int) ((BytesMessage) msg).getBodyLength();
            data = new byte[len];
            tmp.readBytes(data);
        } else if (msg instanceof TextMessage) {
            data = ((TextMessage) msg).getText().getBytes();

        }
    } catch (final JMSException e) {
        logger.error("Error getting bytes from message.", e);
    }
    return data;
}
 
開發者ID:chadbeaudin,項目名稱:DataRecorder,代碼行數:25,代碼來源:SubscriberThread.java

示例3: DataRecorderMessage

import javax.jms.BytesMessage; //導入方法依賴的package包/類
public DataRecorderMessage(final Message message, final long timeStamp) {
    try {
        // this.setDelayMillis(delayMillis);
        this.timeStamp = new Date(timeStamp);

        // Get all the properties from the incoming message
        this.properties = getAllProperties(message);

        // Get the payload from the incoming message.
        if (message instanceof BytesMessage) {
            byte[] byteArray = null;
            final BytesMessage tmp = (BytesMessage) message;
            int len;
            len = (int) ((BytesMessage) message).getBodyLength();
            byteArray = new byte[len];
            tmp.readBytes(byteArray);
            this.body = byteArray;
        } else if (message instanceof TextMessage) {
            this.body = ((TextMessage) message).getText();
        }

    } catch (final JMSException e) {
        logger.error("Error reading from the incoming JMS message");
        logger.error("Stacktrace: ", e);
    }
}
 
開發者ID:chadbeaudin,項目名稱:DataRecorder,代碼行數:27,代碼來源:DataRecorderMessage.java

示例4: convertFromBytesMessage

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

示例5: getPayload

import javax.jms.BytesMessage; //導入方法依賴的package包/類
private String getPayload(Message message) throws Exception {
	String payload = null;
	
	if (message instanceof TextMessage) {
		payload = ((TextMessage) message).getText();
	} 
	else if(message instanceof BytesMessage) {
		BytesMessage bMessage = (BytesMessage) message;
		int payloadLength = (int)bMessage.getBodyLength();
		byte payloadBytes[] = new byte[payloadLength];
		bMessage.readBytes(payloadBytes);
		payload = new String(payloadBytes);
	}
	else {
		log.warn("Message not recognized as a TextMessage or BytesMessage.  It is of type: "+message.getClass().toString());
		payload = message.toString();
	}
	return payload;
}
 
開發者ID:sdeeg-pivotal,項目名稱:rabbitmq-jms-samples,代碼行數:20,代碼來源:SimpleMessageListener.java

示例6: run

import javax.jms.BytesMessage; //導入方法依賴的package包/類
@Override
public void run(SourceContext<OUT> ctx) throws Exception {
    while (runningChecker.isRunning()) {
        exceptionListener.checkErroneous();

        Message message = consumer.receive(1000);
        if (! (message instanceof BytesMessage)) {
            LOG.warn("Active MQ source received non bytes message: {}", message);
            return;
        }
        BytesMessage bytesMessage = (BytesMessage) message;
        byte[] bytes = new byte[(int) bytesMessage.getBodyLength()];
        bytesMessage.readBytes(bytes);
        OUT value = deserializationSchema.deserialize(bytes);
        synchronized (ctx.getCheckpointLock()) {
            ctx.collect(value);
            if (!autoAck) {
                addId(bytesMessage.getJMSMessageID());
                unacknowledgedMessages.put(bytesMessage.getJMSMessageID(), bytesMessage);
            }
        }
    }
}
 
開發者ID:apache,項目名稱:bahir-flink,代碼行數:24,代碼來源:AMQSource.java

示例7: onMessage

import javax.jms.BytesMessage; //導入方法依賴的package包/類
@Override
public void onMessage(Message message) {
    try {
        if (message instanceof BytesMessage) {
            Destination source = message.getJMSDestination();
            BytesMessage bytesMsg = (BytesMessage) message;

            byte[] payload = new byte[(int) bytesMsg.getBodyLength()];
            bytesMsg.readBytes(payload);

            listeners.forEach(listener -> {
                listener.onMessage(source.toString(), payload);
            });
        } else {
            LOG.debug("Received message type we don't yet handle: {}", message);
        }

        // TODO - Handle other message types.

    } catch (Exception ex) {
        LOG.error("Error delivering incoming message to listeners: {}", ex.getMessage());
        LOG.trace("Error detail", ex);
    }
}
 
開發者ID:rhiot,項目名稱:iot-spec,代碼行數:25,代碼來源:QpidJMSTransport.java

示例8: extractBytes

import javax.jms.BytesMessage; //導入方法依賴的package包/類
/**
 * Extracts the byte array from a JMS message.
 * 
 * @param message  the JMS message, not null
 * @return the extracted byte array, not null
 */
public static byte[] extractBytes(final Message message) {
  if (message instanceof BytesMessage == false) {
    throw new IllegalArgumentException("Message must be an instanceof BytesMessage");
  }
  final BytesMessage bytesMessage = (BytesMessage) message;
  final byte[] bytes;
  try {
    long bodyLength = bytesMessage.getBodyLength();
    if (bodyLength > Integer.MAX_VALUE) {
      throw new IllegalArgumentException("Message too large, maximum size is 2GB, received one of length " + bodyLength);
    }
    bytes = new byte[(int) bodyLength];
    bytesMessage.readBytes(bytes);
  } catch (JMSException jmse) {
    throw new JmsRuntimeException(jmse);
  }
  return bytes;
}
 
開發者ID:DevStreet,項目名稱:FinanceAnalytics,代碼行數:25,代碼來源:JmsByteArrayHelper.java

示例9: testBytesMessage_2

import javax.jms.BytesMessage; //導入方法依賴的package包/類
/**
 * Send a <code>BytesMessage</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 testBytesMessage_2() {
   try {
      byte[] bytes = new String("pi").getBytes();
      BytesMessage message = senderSession.createBytesMessage();
      message.writeBytes(bytes);
      message.writeDouble(3.14159);
      sender.send(message);

      Message m = receiver.receive(TestConfig.TIMEOUT);
      Assert.assertTrue("The message should be an instance of BytesMessage.\n", m instanceof BytesMessage);
      BytesMessage msg = (BytesMessage) m;
      byte[] receivedBytes = new byte[bytes.length];
      msg.readBytes(receivedBytes);
      Assert.assertEquals(new String(bytes), new String(receivedBytes));
      Assert.assertEquals(3.14159, msg.readDouble(), 0);
   } catch (JMSException e) {
      fail(e);
   }
}
 
開發者ID:apache,項目名稱:activemq-artemis,代碼行數:27,代碼來源:MessageTypeTest.java

示例10: assertEquivalent

import javax.jms.BytesMessage; //導入方法依賴的package包/類
@Override
protected void assertEquivalent(final Message m, final int mode, final boolean redelivered) throws JMSException {
   super.assertEquivalent(m, mode, redelivered);

   BytesMessage bm = (BytesMessage) m;

   ProxyAssertSupport.assertEquals(true, bm.readBoolean());
   ProxyAssertSupport.assertEquals((byte) 3, bm.readByte());
   byte[] bytes = new byte[3];
   bm.readBytes(bytes);
   ProxyAssertSupport.assertEquals((byte) 4, bytes[0]);
   ProxyAssertSupport.assertEquals((byte) 5, bytes[1]);
   ProxyAssertSupport.assertEquals((byte) 6, bytes[2]);
   ProxyAssertSupport.assertEquals((char) 7, bm.readChar());
   ProxyAssertSupport.assertEquals(new Double(8.0), new Double(bm.readDouble()));
   ProxyAssertSupport.assertEquals(new Float(9.0), new Float(bm.readFloat()));
   ProxyAssertSupport.assertEquals(10, bm.readInt());
   ProxyAssertSupport.assertEquals(11L, bm.readLong());
   ProxyAssertSupport.assertEquals((short) 12, bm.readShort());
   ProxyAssertSupport.assertEquals("this is an UTF String", bm.readUTF());
}
 
開發者ID:apache,項目名稱:activemq-artemis,代碼行數:22,代碼來源:BytesMessageTest.java

示例11: objectFromMsg

import javax.jms.BytesMessage; //導入方法依賴的package包/類
/**
 * Deserialize a JMS message containing a Java serialized abject.
 *
 * @param message JMS input message with a serialized payload
 * @param <T>     Type of the serialized object
 * @return The de-serialized object
 * @throws RuntimeException Can be thrown if deserialization fails.
 */
public static <T extends Serializable> T objectFromMsg(@NotNull final BytesMessage message) {
    requireNonNull(message);

    try {
        byte[] rawData = new byte[(int) message.getBodyLength()];
        message.readBytes(rawData);
        return (T) convertFromBytes(rawData); // NOSONAR
    } catch (javax.jms.JMSException e) {
        throw propagate(e);
    }
}
 
開發者ID:florentw,項目名稱:bench,代碼行數:20,代碼來源:JMSHelper.java

示例12: getPayload

import javax.jms.BytesMessage; //導入方法依賴的package包/類
public static byte[] getPayload(final BytesMessage bytesMessage) throws Exception {

		final int len = Long.valueOf(bytesMessage.getBodyLength()).intValue();
		final byte[] bytes = new byte[len];
		bytesMessage.readBytes(bytes, len);
		return bytes;
	}
 
開發者ID:dcsolutions,項目名稱:kalinka,代碼行數:8,代碼來源:JmsUtil.java

示例13: 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

示例14: getPayloadFromBytesMessage

import javax.jms.BytesMessage; //導入方法依賴的package包/類
/**
 * Convert a BytesMessage to a Java Object with the specified type.
 * @param message the input message
 * @return the message body
 * @throws JMSException if thrown by JMS
 * @throws IOException in case of I/O errors
 */
protected String getPayloadFromBytesMessage(BytesMessage message) 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 {
        return new String(bytes, encoding);
    } catch (UnsupportedEncodingException ex) {
        throw new MessageConversionException("Cannot convert bytes to String", ex);
    }
}
 
開發者ID:kinglcc,項目名稱:spring-boot-jms,代碼行數:22,代碼來源:Jackson2JmsMessageConverter.java

示例15: saveMessagePayload

import javax.jms.BytesMessage; //導入方法依賴的package包/類
private void saveMessagePayload(Message msg, String baseName) throws JMSException, IOException {
    try (FileOutputStream fos = new FileOutputStream(new File(_messageFileDirectory, baseName + ".payload"))) {
        byte[] payload;
        if (msg instanceof TextMessage) {
            payload = TextMessageData.textToBytes(((TextMessage) msg).getText());
        }
        else if (msg instanceof BytesMessage) {
            BytesMessage bytesMessage = (BytesMessage) msg;
            payload = new byte[(int) bytesMessage.getBodyLength()];
            bytesMessage.readBytes(payload);
        }
        else if (msg instanceof ObjectMessage) {
            payload = _objectMessageAdapter.getObjectPayload((ObjectMessage) msg);
        }
        else if (msg instanceof MapMessage) {
            // Partial support, not all data types are handled and we may not be able to post
            MapMessage mapMessage = (MapMessage) msg;
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            Properties props = new Properties();
            for (Enumeration<?> mapNames = mapMessage.getMapNames(); mapNames.hasMoreElements();) {
                String mapName = mapNames.nextElement().toString();
                props.setProperty(mapName, mapMessage.getObject(mapName).toString());
            }
            props.store(bos, "Map message properties for " + msg.getJMSMessageID());
            payload = bos.toByteArray();
        }
        else if (msg instanceof StreamMessage) {
            _logger.warn("Can't save payload for {}, stream messages not supported!", msg.getJMSMessageID());
            payload = new byte[0];
        }
        else {
            _logger.warn("Can't save payload for {}, unsupported type {}!", msg.getJMSMessageID(),
                msg.getClass().getName());
            payload = new byte[0];
        }
        fos.write(payload);
        fos.flush();
    }
}
 
開發者ID:erik-wramner,項目名稱:JmsTools,代碼行數:40,代碼來源:DequeueWorker.java


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