Java Code Examples for javax.jms.Session#createBytesMessage()

The following examples show how to use javax.jms.Session#createBytesMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JmsProduceMessageTypesTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000)
public void testSendJMSBytesMessage() throws Exception {
    connection = createAmqpConnection();
    connection.start();

    String payload = "TEST";

    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    assertNotNull(session);
    Queue queue = session.createQueue(name.getMethodName());
    MessageProducer producer = session.createProducer(queue);
    BytesMessage message = session.createBytesMessage();
    message.writeUTF(payload);
    producer.send(message);
    QueueViewMBean proxy = getProxyToQueue(name.getMethodName());
    assertEquals(1, proxy.getQueueSize());

    MessageConsumer consumer = session.createConsumer(queue);
    Message received = consumer.receive(5000);
    assertNotNull(received);
    assertTrue(received instanceof BytesMessage);
    BytesMessage bytes = (BytesMessage) received;
    assertEquals(payload, bytes.readUTF());
}
 
Example 2
Source File: AdvisoryTests.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
public void testMessageExpiredAdvisory() throws Exception {
   Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   Queue queue = s.createQueue(getClass().getName());
   MessageConsumer consumer = s.createConsumer(queue);
   assertNotNull(consumer);

   Topic advisoryTopic = AdvisorySupport.getExpiredMessageTopic((ActiveMQDestination) queue);
   MessageConsumer advisoryConsumer = s.createConsumer(advisoryTopic);
   //start throwing messages at the consumer
   MessageProducer producer = s.createProducer(queue);
   producer.setTimeToLive(1);
   for (int i = 0; i < MESSAGE_COUNT; i++) {
      BytesMessage m = s.createBytesMessage();
      m.writeBytes(new byte[1024]);
      producer.send(m);
   }

   Message msg = advisoryConsumer.receive(2000);
   assertNotNull(msg);
}
 
Example 3
Source File: JMSWebSocketConnectionTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
protected void doSendLargeMessageViaOpenWire(Connection connection) throws Exception {
   try {
      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      Queue queue = session.createQueue(getQueueName());
      MessageProducer producer = session.createProducer(queue);

      // Normal netty default max frame size is 65535 so bump up the size a bit
      // to see if we can handle it
      byte[] payload = new byte[65535 + 8192];
      for (int i = 0; i < payload.length; ++i) {
         payload[i] = (byte) (i % 256);
      }
      BytesMessage message = session.createBytesMessage();
      message.writeBytes(payload);

      producer.send(message);
   } finally {
      connection.close();
   }
}
 
Example 4
Source File: AdvisoryTempDestinationTests.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
public void testTempMessageConsumedAdvisory() throws Exception {
   Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   TemporaryQueue queue = s.createTemporaryQueue();
   MessageConsumer consumer = s.createConsumer(queue);

   Topic advisoryTopic = AdvisorySupport.getMessageConsumedAdvisoryTopic((ActiveMQDestination) queue);
   MessageConsumer advisoryConsumer = s.createConsumer(advisoryTopic);
   //start throwing messages at the consumer
   MessageProducer producer = s.createProducer(queue);

   BytesMessage m = s.createBytesMessage();
   m.writeBytes(new byte[1024]);
   producer.send(m);
   String id = m.getJMSMessageID();
   Message msg = consumer.receive(1000);
   assertNotNull(msg);

   msg = advisoryConsumer.receive(1000);
   assertNotNull(msg);

   ActiveMQMessage message = (ActiveMQMessage) msg;
   ActiveMQMessage payload = (ActiveMQMessage) message.getDataStructure();
   String originalId = payload.getJMSMessageID();
   assertEquals(originalId, id);
}
 
Example 5
Source File: AdvisoryTests.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
public void testSlowConsumerAdvisory() throws Exception {
   Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   Queue queue = s.createQueue(getClass().getName());
   MessageConsumer consumer = s.createConsumer(queue);
   assertNotNull(consumer);

   Topic advisoryTopic = AdvisorySupport.getSlowConsumerAdvisoryTopic((ActiveMQDestination) queue);
   s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   MessageConsumer advisoryConsumer = s.createConsumer(advisoryTopic);
   // start throwing messages at the consumer
   MessageProducer producer = s.createProducer(queue);
   for (int i = 0; i < MESSAGE_COUNT; i++) {
      BytesMessage m = s.createBytesMessage();
      m.writeBytes(new byte[1024]);
      producer.send(m);
   }
   Message msg = advisoryConsumer.receive(1000);
   assertNotNull(msg);
}
 
Example 6
Source File: FloodPublisher.java    From perf-harness with MIT License 6 votes vote down vote up
public void buildAMQPResources() throws Exception {
 	if (useJMS) {
 		// Use JMS to send messages to a topic.
JmsFactoryFactory ff = JmsFactoryFactory.getInstance(JmsConstants.WMQ_PROVIDER);
JmsConnectionFactory cf = ff.createConnectionFactory();
cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_BINDINGS);
cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, "PERF0");
jmsConnection = cf.createConnection();
jmsConnection.start();
Session jmsSession = jmsConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic jmsTopic = jmsSession.createTopic(inboundTopic);
jmsProducer = jmsSession.createProducer(jmsTopic);
jmsMessage = jmsSession.createBytesMessage();
jmsMessage.writeBytes(message.getBytes());
 	}
 	else {
 		super.buildAMQPResources();
 	}
 }
 
Example 7
Source File: BodyTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@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();
      producer.send(bytesMessage);

      Message msg = cons.receiveNoWait();
      assertNotNull(msg);

      try {
         msg.getBody(String.class);
         fail("Exception expected");
      } catch (MessageFormatException e) {
      }
   }

}
 
Example 8
Source File: GeneralInteropTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
private void sendBytesMessageUsingOpenWire(byte[] bytesData) throws Exception {
   Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   ActiveMQDestination destination = createDestination(session, ActiveMQDestination.QUEUE_TYPE);

   final ActiveMQMessageProducer producer = (ActiveMQMessageProducer) session.createProducer(destination);

   BytesMessage bytesMessage = session.createBytesMessage();
   bytesMessage.writeBytes(bytesData);
   bytesMessage.writeBoolean(true);
   bytesMessage.writeLong(99999L);
   bytesMessage.writeChar('h');
   bytesMessage.writeInt(987);
   bytesMessage.writeShort((short) 1099);
   bytesMessage.writeUTF("hellobytes");

   producer.send(bytesMessage);
}
 
Example 9
Source File: AdvisoryTempDestinationTests.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
public void testSlowConsumerAdvisory() throws Exception {
   Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   TemporaryQueue queue = s.createTemporaryQueue();
   MessageConsumer consumer = s.createConsumer(queue);
   assertNotNull(consumer);

   Topic advisoryTopic = AdvisorySupport.getSlowConsumerAdvisoryTopic((ActiveMQDestination) queue);
   s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   MessageConsumer advisoryConsumer = s.createConsumer(advisoryTopic);
   // start throwing messages at the consumer
   MessageProducer producer = s.createProducer(queue);
   for (int i = 0; i < MESSAGE_COUNT; i++) {
      BytesMessage m = s.createBytesMessage();
      m.writeBytes(new byte[1024]);
      producer.send(m);
   }
   Message msg = advisoryConsumer.receive(1000);
   assertNotNull(msg);
}
 
Example 10
Source File: AdvisoryTests.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
public void xtestMessageDiscardedAdvisory() throws Exception {
   Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   Topic topic = s.createTopic(getClass().getName());
   MessageConsumer consumer = s.createConsumer(topic);
   assertNotNull(consumer);

   Topic advisoryTopic = AdvisorySupport.getMessageDiscardedAdvisoryTopic((ActiveMQDestination) topic);
   MessageConsumer advisoryConsumer = s.createConsumer(advisoryTopic);
   //start throwing messages at the consumer
   MessageProducer producer = s.createProducer(topic);
   int count = (new ActiveMQPrefetchPolicy().getTopicPrefetch() * 2);
   for (int i = 0; i < count; i++) {
      BytesMessage m = s.createBytesMessage();
      producer.send(m);
   }

   Message msg = advisoryConsumer.receive(1000);
   assertNotNull(msg);
}
 
Example 11
Source File: ConnectionIntegrationTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
private void doMaxFrameSizeInfluencesOutgoingFrameSizeTestImpl(int frameSize, int bytesPayloadSize, int numFrames) throws Exception {
    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        testPeer.expectSaslLayerDisabledConnect(equalTo(UnsignedInteger.valueOf(frameSize)));
        // Each connection creates a session for managing temporary destinations etc
        testPeer.expectBegin();

        String uri = "amqp://localhost:" + testPeer.getServerPort() + "?amqp.saslLayer=false&amqp.maxFrameSize=" + frameSize;
        ConnectionFactory factory = new JmsConnectionFactory(uri);
        Connection connection = factory.createConnection();

        testPeer.expectBegin();
        testPeer.expectSenderAttach();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue("myQueue");
        MessageProducer producer = session.createProducer(queue);

        // Expect n-1 transfers of maxFrameSize
        for (int i = 1; i < numFrames; i++) {
            testPeer.expectTransfer(frameSize);
        }
        // Plus one more of unknown size (framing overhead).
        testPeer.expectTransfer(0);

        // Send the message
        byte[] orig = createBytePyload(bytesPayloadSize);
        BytesMessage message = session.createBytesMessage();
        message.writeBytes(orig);

        producer.send(message);

        testPeer.expectClose();
        connection.close();

        testPeer.waitForAllHandlersToComplete(3000);
    }
}
 
Example 12
Source File: SendReceiveMultiThreadTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
   try {

      Connection connection = cf.createConnection();

      Session session = connection.createSession(true, Session.SESSION_TRANSACTED);

      MessageProducer producer = session.createProducer(destination);

      align.countDown();
      start.await();

      for (int i = 0; i < numberOfMessages; i++) {
         BytesMessage msg = session.createBytesMessage();
         msg.writeBytes(new byte[1024]);
         producer.send(msg);
         session.commit();

         int s = sent.incrementAndGet();
         if (s % 1000 == 0) {
            System.out.println("Sent " + s);
         }
      }

      connection.close();
      System.out.println("Send " + numberOfMessages + " messages on thread " + Thread.currentThread().getName());
   } catch (Exception e) {
      e.printStackTrace();
      errors.incrementAndGet();
   } finally {
      finish.countDown();
   }
}
 
Example 13
Source File: MemoryConsumptionTestClient.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
private void publish(int numberOfMessages, int messageSize, int numberOfProducers, int deliveryMode,
                     Destination destination, Map<Connection, List<Session>> connectionsAndSessions) throws JMSException
{
    byte[] messageBytes = generateMessage(messageSize);
    for (List<Session> sessions : connectionsAndSessions.values())
    {
        for (Session session: sessions)
        {
            BytesMessage message = session.createBytesMessage();

            if (messageSize > 0)
            {
                message.writeBytes(messageBytes);
            }

            for(int i = 0; i < numberOfProducers ; i++)
            {
                MessageProducer prod = session.createProducer(destination);
                for(int j = 0; j < numberOfMessages ; j++)
                {
                    prod.send(message, deliveryMode, Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE);
                    if(session.getTransacted())
                    {
                        session.commit();
                    }
                }
            }
        }
    }
}
 
Example 14
Source File: MessageConverterImpl.java    From message-queue-client-framework with Apache License 2.0 5 votes vote down vote up
@Override
  public Message toMessage(Object obj, Session session) throws JMSException,
          MessageConversionException {

      logger.debug("Method : toMessage");

      if (!(obj instanceof MessageBeanImpl)) {

          throw new MessageConversionException("Obj is not MessageBeanImpl.");
      }

/* Jms字节类型消息 */
      BytesMessage bytesMessage = session.createBytesMessage();

      MessageBeanImpl messageBean = (MessageBeanImpl) obj;

      bytesMessage.setStringProperty("MessageType",
              messageBean.getMessageType());
      bytesMessage.setStringProperty("MessageAckNo",
              messageBean.getMessageAckNo());
      bytesMessage.setStringProperty("MessageNo", messageBean.getMessageNo());
      bytesMessage.setLongProperty("MessageDate",
              messageBean.getMessageDate());

      bytesMessage.writeBytes(messageBean.getMessageContent());

      logger.debug("Convert Success, The Send Message No is "
              + messageBean.getMessageNo());

      return bytesMessage;
  }
 
Example 15
Source File: AmqpLargeMessageTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
public void doTestSendLargeMessage(int expectedSize) throws Exception {
   LOG.debug("doTestSendLargeMessage called with expectedSize " + expectedSize);
   byte[] payload = createLargePayload(expectedSize);
   assertEquals(expectedSize, payload.length);

   ConnectionFactory factory = new JmsConnectionFactory("amqp://localhost:61616");
   try (Connection connection = factory.createConnection()) {

      long startTime = System.currentTimeMillis();
      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      Queue queue = session.createQueue(name.getMethodName());
      MessageProducer producer = session.createProducer(queue);
      BytesMessage message = session.createBytesMessage();
      message.writeBytes(payload);
      producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

      // Set this to non-default to get a Header in the encoded message.
      producer.setPriority(4);
      producer.send(message);
      long endTime = System.currentTimeMillis();

      LOG.debug("Returned from send after {} ms", endTime - startTime);
      startTime = System.currentTimeMillis();
      MessageConsumer consumer = session.createConsumer(queue);
      connection.start();

      LOG.debug("Calling receive");
      Message received = consumer.receive();
      assertNotNull(received);
      assertTrue(received instanceof BytesMessage);
      BytesMessage bytesMessage = (BytesMessage) received;
      assertNotNull(bytesMessage);
      endTime = System.currentTimeMillis();

      LOG.debug("Returned from receive after {} ms", endTime - startTime);
      byte[] bytesReceived = new byte[expectedSize];
      assertEquals(expectedSize, bytesMessage.readBytes(bytesReceived, expectedSize));
      assertTrue(Arrays.equals(payload, bytesReceived));
      connection.close();
   }
}
 
Example 16
Source File: JMSTestUtil.java    From cxf with Apache License 2.0 4 votes vote down vote up
/**
 * @param testcase
 * @param session
 * @param rtd
 * @return
 * @throws JMSException
 */
public static Message buildJMSMessageFromTestCase(TestCaseType testcase, Session session,
                                                  Destination rtd) throws JMSException {
    MessagePropertiesType messageProperties = testcase.getRequestMessage();
    Message jmsMessage = null;
    String messageType = messageProperties.getMessageType();
    if ("text".equals(messageType)) {
        jmsMessage = session.createTextMessage();
        ((TextMessage)jmsMessage).setText("test");
    } else if ("byte".equals(messageType)) {
        jmsMessage = session.createBytesMessage();
    } else if ("stream".equals(messageType)) {
        jmsMessage = session.createStreamMessage();
        ((StreamMessage)jmsMessage).writeString("test");
    } else {
        jmsMessage = session.createBytesMessage();
    }

    jmsMessage.setJMSReplyTo(rtd);

    if (messageProperties.isSetDeliveryMode()) {
        jmsMessage.setJMSDeliveryMode(messageProperties.getDeliveryMode());
    }
    if (messageProperties.isSetExpiration()) {
        jmsMessage.setJMSExpiration(messageProperties.getExpiration());
    }
    if (messageProperties.isSetPriority()) {
        jmsMessage.setJMSPriority(messageProperties.getPriority());
    }
    if (messageProperties.isSetExpiration()) {
        jmsMessage.setJMSPriority(messageProperties.getExpiration());
    }
    if (messageProperties.isSetCorrelationID()) {
        jmsMessage.setJMSCorrelationID(messageProperties.getCorrelationID());
    }

    if (messageProperties.isSetTargetService()
        && !"".equals(messageProperties.getTargetService().trim())) {
        jmsMessage.setStringProperty(JMSSpecConstants.TARGETSERVICE_FIELD, messageProperties
            .getTargetService().trim());
    }

    if (messageProperties.isSetBindingVersion()
        && !"".equals(messageProperties.getBindingVersion().trim())) {
        jmsMessage.setStringProperty(JMSSpecConstants.BINDINGVERSION_FIELD, messageProperties
                                     .getBindingVersion().trim());
    }

    if (messageProperties.isSetContentType()
        && !"".equals(messageProperties.getContentType().trim())) {
        jmsMessage.setStringProperty(JMSSpecConstants.CONTENTTYPE_FIELD, messageProperties
            .getContentType().trim());
    }

    if (messageProperties.isSetSoapAction()
        && !"".equals(messageProperties.getSoapAction().trim())) {
        jmsMessage.setStringProperty(JMSSpecConstants.SOAPACTION_FIELD, messageProperties
            .getSoapAction().trim());
    }

    if (messageProperties.isSetRequestURI()
        && !"".equals(messageProperties.getRequestURI().trim())) {
        jmsMessage.setStringProperty(JMSSpecConstants.REQUESTURI_FIELD, messageProperties
            .getRequestURI().trim());
    }
    return jmsMessage;
}
 
Example 17
Source File: SimpleOpenWireTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testCompression() throws Exception {

   Connection cconnection = null;
   Connection connection = null;
   try {
      ActiveMQConnectionFactory cfactory = new ActiveMQConnectionFactory("tcp://" + OWHOST + ":" + OWPORT + "");
      cconnection = cfactory.createConnection();
      cconnection.start();
      Session csession = cconnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      Queue cQueue = csession.createQueue(queueName);
      MessageConsumer consumer = csession.createConsumer(cQueue);

      ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://" + OWHOST + ":" + OWPORT + "?jms.useCompression=true");
      connection = factory.createConnection();
      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      Queue queue = session.createQueue(queueName);

      MessageProducer producer = session.createProducer(queue);
      producer.setDeliveryMode(DeliveryMode.PERSISTENT);

      //text
      TextMessage textMessage = session.createTextMessage();
      textMessage.setText(testString);
      TextMessage receivedMessage = sendAndReceive(textMessage, producer, consumer);

      String receivedText = receivedMessage.getText();
      assertEquals(testString, receivedText);

      //MapMessage
      MapMessage mapMessage = session.createMapMessage();
      mapMessage.setString(testProp, propValue);
      MapMessage receivedMapMessage = sendAndReceive(mapMessage, producer, consumer);
      String value = receivedMapMessage.getString(testProp);
      assertEquals(propValue, value);

      //Object
      ObjectMessage objMessage = session.createObjectMessage();
      objMessage.setObject(testString);
      ObjectMessage receivedObjMessage = sendAndReceive(objMessage, producer, consumer);
      String receivedObj = (String) receivedObjMessage.getObject();
      assertEquals(testString, receivedObj);

      //Stream
      StreamMessage streamMessage = session.createStreamMessage();
      streamMessage.writeString(testString);
      StreamMessage receivedStreamMessage = sendAndReceive(streamMessage, producer, consumer);
      String streamValue = receivedStreamMessage.readString();
      assertEquals(testString, streamValue);

      //byte
      BytesMessage byteMessage = session.createBytesMessage();
      byte[] bytes = testString.getBytes();
      byteMessage.writeBytes(bytes);

      BytesMessage receivedByteMessage = sendAndReceive(byteMessage, producer, consumer);
      long receivedBodylength = receivedByteMessage.getBodyLength();

      assertEquals("bodylength Correct", bytes.length, receivedBodylength);

      byte[] receivedBytes = new byte[(int) receivedBodylength];
      receivedByteMessage.readBytes(receivedBytes);

      String receivedString = new String(receivedBytes);
      assertEquals(testString, receivedString);

      //Message
      Message m = session.createMessage();
      sendAndReceive(m, producer, consumer);
   } finally {
      if (cconnection != null) {
         connection.close();
      }
      if (connection != null) {
         cconnection.close();
      }
   }

}
 
Example 18
Source File: JMSConnectorMessage.java    From bop-bitcoin-client with Apache License 2.0 4 votes vote down vote up
public JMSConnectorMessage (Session session) throws JMSException
{
	message = session.createBytesMessage ();
	this.session = session;
}
 
Example 19
Source File: LargeMessageOverBridgeTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
/**
 * The message won't be large to the client while it will be considered large through the bridge
 *
 * @throws Exception
 */
@Test
public void testSendLargeForBridge() throws Exception {
   createQueue(QUEUE);

   Queue queue = (Queue) context1.lookup("queue/" + QUEUE);

   ActiveMQConnectionFactory cf1 = ActiveMQJMSClient.createConnectionFactoryWithHA(JMSFactoryType.CF, new TransportConfiguration(INVM_CONNECTOR_FACTORY, generateInVMParams(1)));
   cf1.setMinLargeMessageSize(200 * 1024);

   Connection conn1 = cf1.createConnection();
   Session session1 = conn1.createSession(true, Session.SESSION_TRANSACTED);
   MessageProducer prod1 = session1.createProducer(queue);

   Connection conn2 = cf2.createConnection();
   Session session2 = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
   MessageConsumer cons2 = session2.createConsumer(queue);
   conn2.start();

   byte[] bytes = new byte[150 * 1024];

   for (int i = 0; i < bytes.length; i++) {
      bytes[i] = getSamplebyte(i);
   }

   for (int i = 0; i < 10; i++) {
      BytesMessage msg = session1.createBytesMessage();
      msg.writeBytes(bytes);
      prod1.send(msg);
   }

   session1.commit();

   for (int i = 0; i < 5; i++) {
      BytesMessage msg2 = (BytesMessage) cons2.receive(5000);
      assertNotNull(msg2);
      msg2.acknowledge();

      for (int j = 0; j < bytes.length; j++) {
         assertEquals("Position " + i, msg2.readByte(), bytes[j]);
      }
   }

   conn1.close();
   conn2.close();
}
 
Example 20
Source File: MarshallingMessageConverter.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * Marshal the given object to a {@link BytesMessage}.
 * @param object the object to be marshalled
 * @param session current JMS session
 * @param marshaller the marshaller to use
 * @return the resulting message
 * @throws JMSException if thrown by JMS methods
 * @throws IOException in case of I/O errors
 * @throws XmlMappingException in case of OXM mapping errors
 * @see Session#createBytesMessage
 * @see Marshaller#marshal(Object, Result)
 */
protected BytesMessage marshalToBytesMessage(Object object, Session session, Marshaller marshaller)
		throws JMSException, IOException, XmlMappingException {

	ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
	StreamResult streamResult = new StreamResult(bos);
	marshaller.marshal(object, streamResult);
	BytesMessage message = session.createBytesMessage();
	message.writeBytes(bos.toByteArray());
	return message;
}