本文整理匯總了Java中javax.jms.BytesMessage類的典型用法代碼示例。如果您正苦於以下問題:Java BytesMessage類的具體用法?Java BytesMessage怎麽用?Java BytesMessage使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
BytesMessage類屬於javax.jms包,在下文中一共展示了BytesMessage類的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();
}
}
示例2: validateBytesConvertedToBytesMessageOnSend
import javax.jms.BytesMessage; //導入依賴的package包/類
@Test
public void validateBytesConvertedToBytesMessageOnSend() throws Exception {
final String destinationName = "testQueue";
JmsTemplate jmsTemplate = CommonTest.buildJmsTemplateForDestination(false);
JMSPublisher publisher = new JMSPublisher(jmsTemplate, mock(ComponentLog.class));
publisher.publish(destinationName, "hellomq".getBytes());
Message receivedMessage = jmsTemplate.receive(destinationName);
assertTrue(receivedMessage instanceof BytesMessage);
byte[] bytes = new byte[7];
((BytesMessage) receivedMessage).readBytes(bytes);
assertEquals("hellomq", new String(bytes));
((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
}
示例3: toSourceRecord
import javax.jms.BytesMessage; //導入依賴的package包/類
/**
* Convert a message into a Kafka Connect SourceRecord.
*
* @param context the JMS context to use for building messages
* @param topic the Kafka topic
* @param messageBodyJms whether to interpret MQ messages as JMS messages
* @param message the message
*
* @return the Kafka Connect SourceRecord
*
* @throws JMSException Message could not be converted
*/
@Override public SourceRecord toSourceRecord(JMSContext context, String topic, boolean messageBodyJms, Message message) throws JMSException {
byte[] payload;
if (message instanceof BytesMessage) {
payload = message.getBody(byte[].class);
}
else if (message instanceof TextMessage) {
String s = message.getBody(String.class);
payload = s.getBytes(UTF_8);
}
else {
log.error("Unsupported JMS message type {}", message.getClass());
throw new ConnectException("Unsupported JMS message type");
}
SchemaAndValue sv = converter.toConnectData(topic, payload);
return new SourceRecord(null, null, topic, sv.schema(), sv.value());
}
示例4: client_sends_five_messages_to_queue_and_receives
import javax.jms.BytesMessage; //導入依賴的package包/類
@Test
public void client_sends_five_messages_to_queue_and_receives() throws Exception {
try (JMSClient client = server.createClient()) {
DummyListener listener = new DummyListener(5);
client.addQueueListener(DUMMY_QUEUE, listener);
client.startListening();
for (int i = 0; i < 5; i++) {
client.sendToQueue(DUMMY_QUEUE, i + "");
}
List<BytesMessage> bytesMessages = listener.awaitMessages();
assertThat(bytesMessages.size(), is(5));
assertNotNull(bytesMessages.get(0));
// Ensure messages are received in the same order they were sent
for (int i = 0; i < 5; i++) {
assertThat(JMSHelper.objectFromMsg(bytesMessages.get(i)), is(i + ""));
}
}
}
示例5: client_sends_to_topic_and_receives
import javax.jms.BytesMessage; //導入依賴的package包/類
@Test
public void client_sends_to_topic_and_receives() throws Exception {
DummyListener listener = new DummyListener(1);
try (JMSClient client = server.createClient()) {
server.getServer().createTopic(DUMMY_TOPIC);
client.addTopicListener(DUMMY_TOPIC, listener);
client.startListening();
client.sendToTopic(DUMMY_TOPIC, DUMMY_PAYLOAD);
List<BytesMessage> bytesMessages = listener.awaitMessages();
assertReceivedMessageIs(bytesMessages, DUMMY_PAYLOAD);
}
}
示例6: client_sends_five_messages_to_topic_and_receives
import javax.jms.BytesMessage; //導入依賴的package包/類
@Test
public void client_sends_five_messages_to_topic_and_receives() throws Exception {
DummyListener listener = new DummyListener(5);
try (JMSClient client = server.createClient()) {
server.getServer().createTopic(DUMMY_TOPIC);
client.addTopicListener(DUMMY_TOPIC, listener);
client.startListening();
for (int i = 0; i < 5; i++) {
client.sendToTopic(DUMMY_TOPIC, i + "");
}
List<BytesMessage> bytesMessages = listener.awaitMessages();
assertNotNull(server);
assertThat(bytesMessages.size(), is(5));
assertNotNull(bytesMessages.get(0));
// Ensure messages are received in the same order they were sent
for (int i = 0; i < 5; i++) {
assertThat(JMSHelper.objectFromMsg(bytesMessages.get(i)), is(i + ""));
}
}
}
示例7: validateBytesConvertedToBytesMessageOnSendOverJNDI
import javax.jms.BytesMessage; //導入依賴的package包/類
@Test
public void validateBytesConvertedToBytesMessageOnSendOverJNDI() throws Exception {
final String destinationName = "testQueue";
JmsTemplate jmsTemplate = CommonTest.buildJmsJndiTemplateForDestination(false);
JMSPublisher publisher = new JMSPublisher(jmsTemplate, mock(ComponentLog.class));
publisher.publish(destinationName, "hellomq".getBytes());
Message receivedMessage = jmsTemplate.receive(destinationName);
assertTrue(receivedMessage instanceof BytesMessage);
byte[] bytes = new byte[7];
((BytesMessage) receivedMessage).readBytes(bytes);
assertEquals("hellomq", new String(bytes));
((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
}
示例8: validateJmsHeadersAndPropertiesAreTransferredFromFFAttributesOverJNDI
import javax.jms.BytesMessage; //導入依賴的package包/類
@Test
public void validateJmsHeadersAndPropertiesAreTransferredFromFFAttributesOverJNDI() throws Exception {
final String destinationName = "testQueue";
JmsTemplate jmsTemplate = CommonTest.buildJmsJndiTemplateForDestination(false);
JMSPublisher publisher = new JMSPublisher(jmsTemplate, mock(ComponentLog.class));
Map<String, String> flowFileAttributes = new HashMap<>();
flowFileAttributes.put("foo", "foo");
flowFileAttributes.put(JmsHeaders.REPLY_TO, "myTopic");
publisher.publish(destinationName, "hellomq".getBytes(), flowFileAttributes);
Message receivedMessage = jmsTemplate.receive(destinationName);
assertTrue(receivedMessage instanceof BytesMessage);
assertEquals("foo", receivedMessage.getStringProperty("foo"));
assertTrue(receivedMessage.getJMSReplyTo() instanceof Topic);
assertEquals("myTopic", ((Topic) receivedMessage.getJMSReplyTo()).getTopicName());
((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
}
示例9: validateJmsHeadersAndPropertiesAreTransferredFromFFAttributes
import javax.jms.BytesMessage; //導入依賴的package包/類
@Test
public void validateJmsHeadersAndPropertiesAreTransferredFromFFAttributes() throws Exception {
final String destinationName = "testQueue";
JmsTemplate jmsTemplate = CommonTest.buildJmsTemplateForDestination(false);
JMSPublisher publisher = new JMSPublisher(jmsTemplate, mock(ComponentLog.class));
Map<String, String> flowFileAttributes = new HashMap<>();
flowFileAttributes.put("foo", "foo");
flowFileAttributes.put(JmsHeaders.REPLY_TO, "myTopic");
publisher.publish(destinationName, "hellomq".getBytes(), flowFileAttributes);
Message receivedMessage = jmsTemplate.receive(destinationName);
assertTrue(receivedMessage instanceof BytesMessage);
assertEquals("foo", receivedMessage.getStringProperty("foo"));
assertTrue(receivedMessage.getJMSReplyTo() instanceof Topic);
assertEquals("myTopic", ((Topic) receivedMessage.getJMSReplyTo()).getTopicName());
((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
}
示例10: doSendBinaryMessage
import javax.jms.BytesMessage; //導入依賴的package包/類
private void doSendBinaryMessage( final Session session, final Destination destination,
final byte[] bytes,
final Map<String, ?> properties ) throws JMSException {
try {
BytesMessage message = session.createBytesMessage();
message.writeBytes(bytes);
if (properties != null) {
// Note: Setting any properties (including JMS fields) using
// setObjectProperty might not be supported by all providers
// Tested with: ActiveMQ
for (final Entry<String, ?> property : properties.entrySet()) {
message.setObjectProperty(property.getKey(), property.getValue());
}
}
final MessageProducer producer = session.createProducer(destination);
producer.send(message);
} finally {
releaseSession(false);
}
}
示例11: send
import javax.jms.BytesMessage; //導入依賴的package包/類
public void send(byte[] message) {
if (session == null) {
logger.warning("Session is null - sending monitoring message aborted");
return;
}
if (queue == null) {
logger.warning("Queue is null - sending monitoring message aborted");
return;
}
if (producer == null) {
logger.warning("Producer is null - sending monitoring message aborted");
return;
}
try {
BytesMessage bytesMessage = session.createBytesMessage();
bytesMessage.writeBytes(message);
producer.send(bytesMessage);
} catch (JMSException ex) {
logger.log(Level.SEVERE, "Message sending error", ex);
}
//logger.info("Sent monitoring message");
}
示例12: sendMessage
import javax.jms.BytesMessage; //導入依賴的package包/類
@Override
public void sendMessage(final Message message) {
this.jmsTemplate.send(createDestination(message.getDestination()), new MessageCreator() {
@Override
public javax.jms.Message createMessage(Session session) throws JMSException {
BytesMessage bytesMessage = session.createBytesMessage();
bytesMessage.writeBytes(message.getBody());
if(!CollectionUtils.isEmpty(message.getProperties())){
if(message.getProperties().get("JMSXGroupID") != null){
bytesMessage.setStringProperty("JMSXGroupID", message.getProperties().get("JMSXGroupID").toString());
}
if(message.getProperties().get("JMSXGroupSeq") != null){
String JMSXGroupSeq = message.getProperties().get("JMSXGroupSeq").toString();
if(StringUtil.isNumeric(JMSXGroupSeq)){
bytesMessage.setIntProperty("JMSXGroupSeq", Integer.valueOf(JMSXGroupSeq));
}
}
}
return bytesMessage;
}
});
}
示例13: createBytesMessage
import javax.jms.BytesMessage; //導入依賴的package包/類
void createBytesMessage() throws Exception {
BytesMessage message = mock(BytesMessage.class);
when(message.getBodyLength()).thenReturn((long)BYTES.length);
when(message.readBytes(any(byte[].class))).then(new Answer<Integer>() {
@Override
public Integer answer(InvocationOnMock invocation) throws Throwable {
byte[] buffer = (byte[])invocation.getArguments()[0];
if (buffer != null) {
assertEquals(buffer.length, BYTES.length);
System.arraycopy(BYTES, 0, buffer, 0, BYTES.length);
}
return BYTES.length;
}
});
this.message = message;
}
示例14: toMessage
import javax.jms.BytesMessage; //導入依賴的package包/類
/**
*
* @param session
* @param frame
* @return
* @throws JMSException
*/
public Message toMessage(Session session, Frame frame) throws JMSException {
// FIXME buffer pool
final Message msg;
if (frame.contains(CONTENT_LENGTH)) {
final ByteBuffer buf = frame.body().get();
byte[] bytes = new byte[buf.remaining()];
buf.get(bytes);
final BytesMessage bm = session.createBytesMessage();
bm.writeBytes(bytes);
msg = bm;
} else {
msg = session.createTextMessage(UTF_8.decode(frame.body().get()).toString());
}
copyHeaders(session, frame, msg);
return msg;
}
示例15: toMessage_bytes
import javax.jms.BytesMessage; //導入依賴的package包/類
@Test
public void toMessage_bytes() throws JMSException {
final javax.jms.Session session = mock(javax.jms.Session.class);
final Frame frame = mock(Frame.class);
final ByteBuffer buffer = ByteBuffer.wrap(new byte[0]).asReadOnlyBuffer();
when(frame.body()).thenReturn(Optional.of(buffer));
when(frame.headers()).thenReturn(new MultivaluedHashMap<>());
when(frame.contains(Standard.CONTENT_LENGTH)).thenReturn(true);
final BytesMessage message = mock(BytesMessage.class);
when(session.createBytesMessage()).thenReturn(message);
this.factory.toMessage(session, frame);
verify(frame).body();
verify(frame, times(2)).headers();
verify(frame).contains(Standard.CONTENT_LENGTH);
verify(session).createBytesMessage();
verify(message).setJMSCorrelationID(null);
verify(message).writeBytes(new byte[0]);
verifyNoMoreInteractions(session, frame, message);
}