Java Code Examples for javax.jms.MessageProducer#setDisableMessageID()

The following examples show how to use javax.jms.MessageProducer#setDisableMessageID() . 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: MessageProducerFactory.java    From artemis-disruptor-miaosha with Apache License 2.0 6 votes vote down vote up
public static MessageProducer createMessageProducer(
    Session session,
    Destination destination,
    MessageProducerOption producerOption) throws JMSException {

  MessageProducer producer = session.createProducer(destination);
  producer.setDeliveryDelay(producerOption.getDeliveryDelay());
  producer.setDeliveryMode(producerOption.getDeliveryMode());
  producer.setDisableMessageTimestamp(producerOption.isDisableMessageTimestamp());
  producer.setDisableMessageID(producerOption.isDisableMessageId());
  producer.setPriority(producerOption.getPriority());
  producer.setTimeToLive(producerOption.getTimeToLive());

  return producer;

}
 
Example 2
Source File: MultipleProducersTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
private void sendMessage(Queue queue, Session session) throws Exception {

      MessageProducer mp = session.createProducer(queue);

      try {
         mp.setDisableMessageID(true);
         mp.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
         mp.setPriority(Message.DEFAULT_PRIORITY);
         mp.setTimeToLive(Message.DEFAULT_TIME_TO_LIVE);

         mp.send(session.createTextMessage("This is message for " + queue.getQueueName()));
      } finally {

         mp.close();
      }
   }
 
Example 3
Source File: NetworkLoadTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
public ForwardingClient(int from, int to) throws JMSException {
   toConnection = createConnection(from);
   Session toSession = toConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   final MessageProducer producer = toSession.createProducer(new ActiveMQQueue("Q" + to));
   producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
   producer.setDisableMessageID(true);

   fromConnection = createConnection(from);
   Session fromSession = fromConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   MessageConsumer consumer = fromSession.createConsumer(new ActiveMQQueue("Q" + from));

   consumer.setMessageListener(new MessageListener() {
      @Override
      public void onMessage(Message msg) {
         try {
            producer.send(msg);
            forwardCounter.incrementAndGet();
         } catch (JMSException e) {
            // this is caused by the connection getting closed.
         }
      }
   });
}
 
Example 4
Source File: JMSHeadersAndPropertiesTest.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
@Test
public void disableJMSMessageId() throws Exception
{
    Queue queue = createQueue(getTestName());
    Connection connection = getConnection();
    try
    {
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(queue);
        Message message = session.createMessage();
        producer.send(message);
        assertNotNull("Produced message is expected to have a JMSMessageID", message.getJMSMessageID());

        final MessageConsumer consumer = session.createConsumer(queue);
        connection.start();

        final Message receivedMessageWithId = consumer.receive(getReceiveTimeout());
        assertNotNull(receivedMessageWithId);

        assertNotNull("Received message is expected to have a JMSMessageID", receivedMessageWithId.getJMSMessageID());
        assertEquals("Received message JMSMessageID should match the sent", message.getJMSMessageID(), receivedMessageWithId.getJMSMessageID());

        producer.setDisableMessageID(true);
        producer.send(message);
        assertNull("Produced message is expected to not have a JMSMessageID", message.getJMSMessageID());

        final Message receivedMessageWithoutId = consumer.receive(getReceiveTimeout());
        assertNotNull(receivedMessageWithoutId);
        assertNull("Received message is not expected to have a JMSMessageID", receivedMessageWithoutId.getJMSMessageID());
    }
    finally
    {
        connection.close();
    }
}
 
Example 5
Source File: JmsMessageProducerTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 10000)
public void testGetDisableMessageID() throws Exception {
    MessageProducer producer = session.createProducer(null);
    assertFalse(producer.getDisableMessageID());
    producer.setDisableMessageID(true);
    assertTrue(producer.getDisableMessageID());
}
 
Example 6
Source File: PerfBase.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
private void sendMessages(final int numberOfMessages,
                          final int txBatchSize,
                          final boolean durable,
                          final boolean transacted,
                          final boolean display,
                          final int throttleRate,
                          final int messageSize) throws Exception {
   MessageProducer producer = session.createProducer(destination);

   producer.setDeliveryMode(perfParams.isDurable() ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT);

   producer.setDisableMessageID(perfParams.isDisableMessageID());

   producer.setDisableMessageTimestamp(perfParams.isDisableTimestamp());

   BytesMessage message = session.createBytesMessage();

   byte[] payload = PerfBase.randomByteArray(messageSize);

   message.writeBytes(payload);

   final int modulo = 2000;

   TokenBucketLimiter tbl = throttleRate != -1 ? new TokenBucketLimiterImpl(throttleRate, false) : null;

   boolean committed = false;
   for (int i = 1; i <= numberOfMessages; i++) {
      producer.send(message);

      if (transacted) {
         if (i % txBatchSize == 0) {
            session.commit();
            committed = true;
         } else {
            committed = false;
         }
      }

      if (display && i % modulo == 0) {
         double duration = (1.0 * System.currentTimeMillis() - start) / 1000;
         PerfBase.log.info(String.format("sent %6d messages in %2.2fs", i, duration));
      }

      if (tbl != null) {
         tbl.limit();
      }
   }
   if (transacted && !committed) {
      session.commit();
   }
}
 
Example 7
Source File: TestBrokerConnectionDuplexExcludedDestinations.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
public void testDuplexSendFromHubToSpoke() throws Exception {

      //create hub producer
      MessageProducer hubProducer = hubSession.createProducer(null);
      hubProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
      hubProducer.setDisableMessageID(true);
      hubProducer.setDisableMessageTimestamp(true);

      //create spoke producer
      MessageProducer spokeProducer = hubSession.createProducer(null);
      spokeProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
      spokeProducer.setDisableMessageID(true);
      spokeProducer.setDisableMessageTimestamp(true);

      Queue excludedQueueHub = hubSession.createQueue("exclude.test.foo");
      TextMessage excludedMsgHub = hubSession.createTextMessage();
      excludedMsgHub.setText(excludedQueueHub.toString());

      Queue includedQueueHub = hubSession.createQueue("include.test.foo");

      TextMessage includedMsgHub = hubSession.createTextMessage();
      includedMsgHub.setText(includedQueueHub.toString());

      Queue alwaysIncludedQueueHub = hubSession.createQueue("always.include.test.foo");

      TextMessage alwaysIncludedMsgHub = hubSession.createTextMessage();
      alwaysIncludedMsgHub.setText(alwaysIncludedQueueHub.toString());

      // Sending from Hub queue
      hubProducer.send(excludedQueueHub, excludedMsgHub);
      hubProducer.send(includedQueueHub, includedMsgHub);
      hubProducer.send(alwaysIncludedQueueHub, alwaysIncludedMsgHub);

      Queue excludedQueueSpoke = spokeSession.createQueue("exclude.test.foo");
      MessageConsumer excludedConsumerSpoke = spokeSession.createConsumer(excludedQueueSpoke);

      Thread.sleep(100);

      Queue includedQueueSpoke = spokeSession.createQueue("include.test.foo");
      MessageConsumer includedConsumerSpoke = spokeSession.createConsumer(includedQueueSpoke);

      Thread.sleep(100);

      Queue alwaysIncludedQueueSpoke = spokeSession.createQueue("always.include.test.foo");
      MessageConsumer alwaysIncludedConsumerSpoke = spokeSession.createConsumer(alwaysIncludedQueueHub);

      Thread.sleep(100);
      TextMessage alwaysIncludedMsgSpoke = spokeSession.createTextMessage();
      alwaysIncludedMsgSpoke.setText(alwaysIncludedQueueSpoke.toString());
      spokeProducer.send(alwaysIncludedQueueSpoke, alwaysIncludedMsgSpoke);

      MessageConsumer alwaysIncludedConsumerHub = spokeSession.createConsumer(alwaysIncludedQueueHub);
      assertNotNull(alwaysIncludedConsumerHub);

      // Receiving from excluded Spoke queue
      Message msg = excludedConsumerSpoke.receive(200);
      assertNull(msg);

      // Receiving from included Spoke queue
      msg = includedConsumerSpoke.receive(200);
      assertEquals(includedMsgHub, msg);

      // Receiving from included Spoke queue
      msg = alwaysIncludedConsumerSpoke.receive(200);
      assertEquals(alwaysIncludedMsgHub, msg);

      // we should be able to receive excluded queue message on Hub
      MessageConsumer excludedConsumerHub = hubSession.createConsumer(excludedQueueHub);
      msg = excludedConsumerHub.receive(200);
      assertEquals(excludedMsgHub, msg);

      hubProducer.close();
      excludedConsumerSpoke.close();
   }
 
Example 8
Source File: ProducerIntegrationTest.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
private void doSendingMessageWithDisableMessageIDHintTestImpl(boolean existingId) throws JMSException, InterruptedException, Exception, IOException {
    try(TestAmqpPeer testPeer = new TestAmqpPeer();) {
        Connection connection = testFixture.establishConnecton(testPeer);
        testPeer.expectBegin();
        testPeer.expectSenderAttach();

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

        String text = "myMessage";
        MessageHeaderSectionMatcher headersMatcher = new MessageHeaderSectionMatcher(true).withDurable(equalTo(true));
        MessageAnnotationsSectionMatcher msgAnnotationsMatcher = new MessageAnnotationsSectionMatcher(true);
        MessagePropertiesSectionMatcher propsMatcher = new MessagePropertiesSectionMatcher(true);
        propsMatcher.withMessageId(nullValue()); // Check there is no message-id value;
        TransferPayloadCompositeMatcher messageMatcher = new TransferPayloadCompositeMatcher();
        messageMatcher.setHeadersMatcher(headersMatcher);
        messageMatcher.setMessageAnnotationsMatcher(msgAnnotationsMatcher);
        messageMatcher.setPropertiesMatcher(propsMatcher);
        messageMatcher.setMessageContentMatcher(new EncodedAmqpValueMatcher(text));
        testPeer.expectTransfer(messageMatcher);
        testPeer.expectClose();

        Message message = session.createTextMessage(text);

        assertNull("JMSMessageID should not yet be set", message.getJMSMessageID());

        if (existingId) {
            // [erroneously] set a JMSMessageID value
            String existingMessageId = "ID:this-should-be-overwritten-in-send";
            message.setJMSMessageID(existingMessageId);
            assertEquals("JMSMessageID should now be set", existingMessageId, message.getJMSMessageID());
        }

        producer.setDisableMessageID(true);
        producer.send(message);

        assertNull("JMSMessageID should be null", message.getJMSMessageID());

        connection.close();

        testPeer.waitForAllHandlersToComplete(2000);
    }
}
 
Example 9
Source File: ForeignMessageIntegrationTest.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
/**
 * Test that after sending a message with the disableMessageID hint set, which already had
 * a JMSMessageID value, that the message object then has a null JMSMessageID value, and no
 * message-id field value was set.
 *
 * @throws Exception if an error occurs during the test.
 */
@Test(timeout = 20000)
public void testSendForeignMessageWithDisableMessageIDHintAndExistingMessageID() throws Exception {
    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        Connection connection = testFixture.establishConnecton(testPeer);
        testPeer.expectBegin();
        testPeer.expectSenderAttach();

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

        byte[] content = "myBytes".getBytes();

        MessageHeaderSectionMatcher headersMatcher = new MessageHeaderSectionMatcher(true).withDurable(equalTo(true));
        MessageAnnotationsSectionMatcher msgAnnotationsMatcher = new MessageAnnotationsSectionMatcher(true);
        MessagePropertiesSectionMatcher propertiesMatcher = new MessagePropertiesSectionMatcher(true);
        propertiesMatcher.withMessageId(nullValue()); // Check there is no message-id value;
        TransferPayloadCompositeMatcher messageMatcher = new TransferPayloadCompositeMatcher();
        messageMatcher.setHeadersMatcher(headersMatcher);
        messageMatcher.setMessageAnnotationsMatcher(msgAnnotationsMatcher);
        messageMatcher.setPropertiesMatcher(propertiesMatcher);
        messageMatcher.setMessageContentMatcher(new EncodedDataMatcher(new Binary(content)));

        testPeer.expectTransfer(messageMatcher);

        // Create a foreign message, [erroneously] set a JMSMessageID value
        ForeignJmsBytesMessage foreign = new ForeignJmsBytesMessage();
        foreign.writeBytes(content);

        assertNull("JMSMessageID should not yet be set", foreign.getJMSMessageID());
        String existingMessageId = "ID:this-should-be-overwritten-in-send";
        foreign.setJMSMessageID(existingMessageId);
        assertEquals("JMSMessageID should now be set", existingMessageId, foreign.getJMSMessageID());

        // Toggle the DisableMessageID hint, then send it
        producer.setDisableMessageID(true);
        producer.send(foreign);

        assertNull("JMSMessageID should now be null", foreign.getJMSMessageID());

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

        testPeer.waitForAllHandlersToComplete(2000);
    }
}
 
Example 10
Source File: JmsTemplate.java    From spring-analysis-note with MIT License 3 votes vote down vote up
/**
 * Create a JMS MessageProducer for the given Session and Destination,
 * configuring it to disable message ids and/or timestamps (if necessary).
 * <p>Delegates to {@link #doCreateProducer} for creation of the raw
 * JMS MessageProducer.
 * @param session the JMS Session to create a MessageProducer for
 * @param destination the JMS Destination to create a MessageProducer for
 * @return the new JMS MessageProducer
 * @throws JMSException if thrown by JMS API methods
 * @see #setMessageIdEnabled
 * @see #setMessageTimestampEnabled
 */
protected MessageProducer createProducer(Session session, @Nullable Destination destination) throws JMSException {
	MessageProducer producer = doCreateProducer(session, destination);
	if (!isMessageIdEnabled()) {
		producer.setDisableMessageID(true);
	}
	if (!isMessageTimestampEnabled()) {
		producer.setDisableMessageTimestamp(true);
	}
	return producer;
}
 
Example 11
Source File: JmsTemplate.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * Create a JMS MessageProducer for the given Session and Destination,
 * configuring it to disable message ids and/or timestamps (if necessary).
 * <p>Delegates to {@link #doCreateProducer} for creation of the raw
 * JMS MessageProducer.
 * @param session the JMS Session to create a MessageProducer for
 * @param destination the JMS Destination to create a MessageProducer for
 * @return the new JMS MessageProducer
 * @throws JMSException if thrown by JMS API methods
 * @see #setMessageIdEnabled
 * @see #setMessageTimestampEnabled
 */
protected MessageProducer createProducer(Session session, @Nullable Destination destination) throws JMSException {
	MessageProducer producer = doCreateProducer(session, destination);
	if (!isMessageIdEnabled()) {
		producer.setDisableMessageID(true);
	}
	if (!isMessageTimestampEnabled()) {
		producer.setDisableMessageTimestamp(true);
	}
	return producer;
}
 
Example 12
Source File: JmsTemplate.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * Create a JMS MessageProducer for the given Session and Destination,
 * configuring it to disable message ids and/or timestamps (if necessary).
 * <p>Delegates to {@link #doCreateProducer} for creation of the raw
 * JMS MessageProducer.
 * @param session the JMS Session to create a MessageProducer for
 * @param destination the JMS Destination to create a MessageProducer for
 * @return the new JMS MessageProducer
 * @throws JMSException if thrown by JMS API methods
 * @see #setMessageIdEnabled
 * @see #setMessageTimestampEnabled
 */
protected MessageProducer createProducer(Session session, Destination destination) throws JMSException {
	MessageProducer producer = doCreateProducer(session, destination);
	if (!isMessageIdEnabled()) {
		producer.setDisableMessageID(true);
	}
	if (!isMessageTimestampEnabled()) {
		producer.setDisableMessageTimestamp(true);
	}
	return producer;
}