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


Java BytesMessage類代碼示例

本文整理匯總了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();
  }
}
 
開發者ID:ppatierno,項目名稱:amqp-kafka-demo,代碼行數:24,代碼來源:Receiver.java

示例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();
}
 
開發者ID:lsac,項目名稱:nifi-jms-jndi,代碼行數:17,代碼來源:JMSPublisherConsumerTest.java

示例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());
}
 
開發者ID:ibm-messaging,項目名稱:kafka-connect-mq-source,代碼行數:30,代碼來源:JsonRecordBuilder.java

示例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 + ""));
        }
    }
}
 
開發者ID:florentw,項目名稱:bench,代碼行數:22,代碼來源:JMSClientServerTest.java

示例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);
    }
}
 
開發者ID:florentw,項目名稱:bench,代碼行數:17,代碼來源:JMSClientServerTest.java

示例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 + ""));
        }
    }
}
 
開發者ID:florentw,項目名稱:bench,代碼行數:26,代碼來源:JMSClientServerTest.java

示例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();
}
 
開發者ID:lsac,項目名稱:nifi-jms-jndi,代碼行數:17,代碼來源:JMSPublisherConsumerTest.java

示例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();
}
 
開發者ID:lsac,項目名稱:nifi-jms-jndi,代碼行數:20,代碼來源:JMSPublisherConsumerTest.java

示例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();
}
 
開發者ID:lsac,項目名稱:nifi-jms-jndi,代碼行數:20,代碼來源:JMSPublisherConsumerTest.java

示例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);
    }
}
 
開發者ID:Axway,項目名稱:ats-framework,代碼行數:22,代碼來源:JmsClient.java

示例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");
}
 
開發者ID:roscisz,項目名稱:KernelHive,代碼行數:23,代碼來源:MonitoringMessageSender.java

示例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;
		}
	});
}
 
開發者ID:yanghuijava,項目名稱:elephant,代碼行數:23,代碼來源:ActivemqProducerService.java

示例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;
}
 
開發者ID:moueimei,項目名稱:flume-release-1.7.0,代碼行數:17,代碼來源:TestDefaultJMSMessageConverter.java

示例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;
}
 
開發者ID:dansiviter,項目名稱:cito,代碼行數:24,代碼來源:Factory.java

示例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);
}
 
開發者ID:dansiviter,項目名稱:cito,代碼行數:22,代碼來源:FactoryTest.java


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