本文整理汇总了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();
}
示例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);
}
示例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();
}
}
示例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));
}
}
示例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);
}
}
示例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");
}
}
示例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());
}
}
示例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);
}