Search in sources :

Example 46 with MessageProducer

use of javax.jms.MessageProducer in project beam by apache.

the class JmsIOTest method testReadMessages.

@Test
public void testReadMessages() throws Exception {
    // produce message
    Connection connection = connectionFactory.createConnection(USERNAME, PASSWORD);
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageProducer producer = session.createProducer(session.createQueue(QUEUE));
    TextMessage message = session.createTextMessage("This Is A Test");
    producer.send(message);
    producer.send(message);
    producer.send(message);
    producer.send(message);
    producer.send(message);
    producer.send(message);
    producer.close();
    session.close();
    connection.close();
    // read from the queue
    PCollection<JmsRecord> output = pipeline.apply(JmsIO.read().withConnectionFactory(connectionFactory).withQueue(QUEUE).withUsername(USERNAME).withPassword(PASSWORD).withMaxNumRecords(5));
    PAssert.thatSingleton(output.apply("Count", Count.<JmsRecord>globally())).isEqualTo(new Long(5));
    pipeline.run();
    connection = connectionFactory.createConnection(USERNAME, PASSWORD);
    session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createConsumer(session.createQueue(QUEUE));
    Message msg = consumer.receiveNoWait();
    assertNull(msg);
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 47 with MessageProducer

use of javax.jms.MessageProducer in project spring-framework by spring-projects.

the class JmsInvokerClientInterceptor method doExecuteRequest.

/**
	 * Actually execute the given request, sending the invoker request message
	 * to the specified target queue and waiting for a corresponding response.
	 * <p>The default implementation is based on standard JMS send/receive,
	 * using a {@link javax.jms.TemporaryQueue} for receiving the response.
	 * @param session the JMS Session to use
	 * @param queue the resolved target Queue to send to
	 * @param requestMessage the JMS Message to send
	 * @return the RemoteInvocationResult object
	 * @throws JMSException in case of JMS failure
	 */
protected Message doExecuteRequest(Session session, Queue queue, Message requestMessage) throws JMSException {
    TemporaryQueue responseQueue = null;
    MessageProducer producer = null;
    MessageConsumer consumer = null;
    try {
        responseQueue = session.createTemporaryQueue();
        producer = session.createProducer(queue);
        consumer = session.createConsumer(responseQueue);
        requestMessage.setJMSReplyTo(responseQueue);
        producer.send(requestMessage);
        long timeout = getReceiveTimeout();
        return (timeout > 0 ? consumer.receive(timeout) : consumer.receive());
    } finally {
        JmsUtils.closeMessageConsumer(consumer);
        JmsUtils.closeMessageProducer(producer);
        if (responseQueue != null) {
            responseQueue.delete();
        }
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) TemporaryQueue(javax.jms.TemporaryQueue) MessageProducer(javax.jms.MessageProducer)

Example 48 with MessageProducer

use of javax.jms.MessageProducer in project spring-framework by spring-projects.

the class JmsInvokerServiceExporter method writeRemoteInvocationResult.

/**
	 * Send the given RemoteInvocationResult as a JMS message to the originator.
	 * @param requestMessage current request message
	 * @param session the JMS Session to use
	 * @param result the RemoteInvocationResult object
	 * @throws javax.jms.JMSException if thrown by trying to send the message
	 */
protected void writeRemoteInvocationResult(Message requestMessage, Session session, RemoteInvocationResult result) throws JMSException {
    Message response = createResponseMessage(requestMessage, session, result);
    MessageProducer producer = session.createProducer(requestMessage.getJMSReplyTo());
    try {
        producer.send(response);
    } finally {
        JmsUtils.closeMessageProducer(producer);
    }
}
Also used : Message(javax.jms.Message) MessageProducer(javax.jms.MessageProducer)

Example 49 with MessageProducer

use of javax.jms.MessageProducer in project spring-framework by spring-projects.

the class JmsTemplate method doSend.

/**
	 * Send the given JMS message.
	 * @param session the JMS Session to operate on
	 * @param destination the JMS Destination to send to
	 * @param messageCreator callback to create a JMS Message
	 * @throws JMSException if thrown by JMS API methods
	 */
protected void doSend(Session session, Destination destination, MessageCreator messageCreator) throws JMSException {
    Assert.notNull(messageCreator, "MessageCreator must not be null");
    MessageProducer producer = createProducer(session, destination);
    try {
        Message message = messageCreator.createMessage(session);
        if (logger.isDebugEnabled()) {
            logger.debug("Sending created message: " + message);
        }
        doSend(producer, message);
        // Check commit - avoid commit call within a JTA transaction.
        if (session.getTransacted() && isSessionLocallyTransacted(session)) {
            // Transacted session created by this template -> commit.
            JmsUtils.commitIfNecessary(session);
        }
    } finally {
        JmsUtils.closeMessageProducer(producer);
    }
}
Also used : Message(javax.jms.Message) MessageProducer(javax.jms.MessageProducer)

Example 50 with MessageProducer

use of javax.jms.MessageProducer in project spring-framework by spring-projects.

the class JmsTemplateTests method testProducerCallbackWithIdAndTimestampDisabled.

@Test
public void testProducerCallbackWithIdAndTimestampDisabled() throws Exception {
    JmsTemplate template = createTemplate();
    template.setConnectionFactory(connectionFactory);
    template.setMessageIdEnabled(false);
    template.setMessageTimestampEnabled(false);
    MessageProducer messageProducer = mock(MessageProducer.class);
    given(session.createProducer(null)).willReturn(messageProducer);
    given(messageProducer.getPriority()).willReturn(4);
    template.execute(new ProducerCallback<Void>() {

        @Override
        public Void doInJms(Session session, MessageProducer producer) throws JMSException {
            session.getTransacted();
            producer.getPriority();
            return null;
        }
    });
    verify(messageProducer).setDisableMessageID(true);
    verify(messageProducer).setDisableMessageTimestamp(true);
    verify(messageProducer).close();
    verify(session).close();
    verify(connection).close();
}
Also used : JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session) Test(org.junit.Test)

Aggregations

MessageProducer (javax.jms.MessageProducer)184 Session (javax.jms.Session)131 TextMessage (javax.jms.TextMessage)74 Message (javax.jms.Message)64 Connection (javax.jms.Connection)60 Test (org.junit.Test)54 MessageConsumer (javax.jms.MessageConsumer)49 JMSException (javax.jms.JMSException)46 Topic (javax.jms.Topic)34 Destination (javax.jms.Destination)31 Queue (javax.jms.Queue)21 ConnectionFactory (javax.jms.ConnectionFactory)12 CountDownLatch (java.util.concurrent.CountDownLatch)10 ObjectMessage (javax.jms.ObjectMessage)10 ModelNode (org.jboss.dmr.ModelNode)10 BytesMessage (javax.jms.BytesMessage)9 QueueSession (javax.jms.QueueSession)9 TemporaryQueue (javax.jms.TemporaryQueue)9 QueueConnection (javax.jms.QueueConnection)7 InitialContext (javax.naming.InitialContext)7