下面列出了怎么用javax.jms.StreamMessage的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 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;
}
}
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();
}
@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);
}
/**
* 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;
}
}
@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());
}
}
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();
}
/**
* 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);
}
}
@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");
}
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[]{};
}
}
@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");
}
@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());
}
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();
}
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();
}
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();
}
/**
* 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);
}
public StreamMessage createStreamMessage() {
StreamMessage streamMessage = null;
try {
streamMessage = this.session.createStreamMessage();
} catch (JMSException e) {
log.error("Error while creating message", e);
Assert.fail();
}
return streamMessage;
}
public static byte[] createByteArray(final Message message) throws JMSException {
if (message instanceof TextMessage) {
return getMessageBytes((TextMessage) message);
} else if (message instanceof BytesMessage) {
return getMessageBytes((BytesMessage) message);
} else if (message instanceof StreamMessage) {
return getMessageBytes((StreamMessage) message);
} else if (message instanceof MapMessage) {
return getMessageBytes((MapMessage) message);
} else if (message instanceof ObjectMessage) {
return getMessageBytes((ObjectMessage) message);
}
return new byte[0];
}
@Override
public StreamMessage createStreamMessage() {
try {
return getSession().createStreamMessage();
} catch (JMSException jmse) {
throw JMSExceptionSupport.createRuntimeException(jmse);
}
}
@Override
public StreamMessage createStreamMessage() {
try {
return getSession().createStreamMessage();
} catch (JMSException jmse) {
throw JMSExceptionSupport.createRuntimeException(jmse);
}
}
@Test
public void createStreamMessage() throws Exception {
StreamMessage message = mock(StreamMessage.class);
keys.stream().forEach(x -> current().put(x, x));
when(delegate.createStreamMessage()).thenReturn(message);
propagator.createStreamMessage();
verify(delegate).createStreamMessage();
verify(message).setStringProperty("1", "1");
}
@Test
public void publishListMessage() throws Exception
{
final List<Object> content = new ArrayList<>();
content.add("astring");
content.add(Integer.MIN_VALUE);
content.add(Long.MAX_VALUE);
content.add(null);
StreamMessage message = publishMessageWithContent(content, StreamMessage.class);
assertThat(message.readString(), is(equalTo("astring")));
assertThat(message.readInt(), is(equalTo((Integer.MIN_VALUE))));
assertThat(message.readLong(), is(equalTo(Long.MAX_VALUE)));
assertThat(message.readObject(), is(nullValue()));
}
private static Object getPayload(Message message) throws JMSException {
if (message == null) {
return null;
}
else if (message instanceof TextMessage) {
return ((TextMessage) message).getText();
}
else if (message instanceof StreamMessage) {
return ((StreamMessage) message).readObject();
}
else if (message instanceof ObjectMessage) {
return ((ObjectMessage) message).getObject();
}
return message.getBody(Object.class);
}
private Object getPayload(Message message) throws JMSException {
if (message == null) {
return null;
}
else if (message instanceof TextMessage) {
return ((TextMessage) message).getText();
}
else if (message instanceof StreamMessage) {
return ((StreamMessage) message).readObject();
}
else if (message instanceof ObjectMessage) {
return ((ObjectMessage) message).getObject();
}
return message.getBody(Object.class);
}
@Override
public String convert(Message message) throws JMSException
{
if (message instanceof TextMessage) {
return ((TextMessage)message).getText();
} else if (message instanceof StreamMessage) {
return ((StreamMessage)message).readString();
} else {
throw new IllegalArgumentException("Unhandled message type " + message.getClass().getName());
}
}
private void createStreamMsgs(int numMessages) throws Exception
{
Long value = 1013L;
StreamMessage message = testMeta.session.createStreamMessage();
message.writeObject(value);
for (int i = 0; i < numMessages; i++) {
testMeta.producer.send(message);
}
}
private void receiveStreamMessage(boolean useCore) throws Exception {
StreamMessage streamMessage = (StreamMessage) receiveMessage(useCore);
boolean booleanVal = streamMessage.readBoolean();
assertTrue(booleanVal);
byte byteVal = streamMessage.readByte();
assertEquals((byte) 10, byteVal);
byte[] originVal = TEXT.getBytes();
byte[] bytesVal = new byte[originVal.length];
streamMessage.readBytes(bytesVal);
for (int i = 0; i < bytesVal.length; i++) {
assertTrue(bytesVal[i] == originVal[i]);
}
char charVal = streamMessage.readChar();
assertEquals('A', charVal);
double doubleVal = streamMessage.readDouble();
assertEquals(55.3D, doubleVal, 0.1D);
float floatVal = streamMessage.readFloat();
assertEquals(79.1F, floatVal, 0.1F);
int intVal = streamMessage.readInt();
assertEquals(37, intVal);
long longVal = streamMessage.readLong();
assertEquals(56652L, longVal);
Object objectVal = streamMessage.readObject();
Object origVal = new String("VVVV");
assertTrue(objectVal.equals(origVal));
short shortVal = streamMessage.readShort();
assertEquals((short) 333, shortVal);
String strVal = streamMessage.readString();
assertEquals(TEXT, strVal);
}
@Override
public StreamMessage createStreamMessage() {
try {
return getSession().createStreamMessage();
} catch (JMSException jmse) {
throw JmsExceptionSupport.createRuntimeException(jmse);
}
}
/**
* Send a <code>StreamMessage</code> with an empty body.
* <br />
* Receive it and test if the message is effectively an instance of
* <code>StreamMessage</code>
*/
@Test
public void testStreamMessage_1() {
try {
StreamMessage message = senderSession.createStreamMessage();
sender.send(message);
Message msg = receiver.receive(TestConfig.TIMEOUT);
Assert.assertTrue("The message should be an instance of StreamMessage.\n", msg instanceof StreamMessage);
} catch (JMSException e) {
fail(e);
}
}
@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();
}
/**
* 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();
}