Search in sources :

Example 6 with MessageProducer

use of javax.jms.MessageProducer in project wildfly by wildfly.

the class HelloBean method sendMessage.

public String sendMessage() throws Exception {
    String destinationName = "java:jboss/exported/queue/TestQueue";
    Context ic = null;
    ConnectionFactory cf = null;
    Connection connection = null;
    try {
        ic = getInitialContext();
        cf = (ConnectionFactory) ic.lookup("java:/ConnectionFactory");
        Queue queue = (Queue) ic.lookup(destinationName);
        connection = cf.createConnection("guest", "guest");
        // we need to start connection for consumer
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer sender = session.createProducer(queue);
        TextMessage message = session.createTextMessage("hello goodbye");
        TemporaryQueue replyQueue = session.createTemporaryQueue();
        message.setJMSReplyTo(replyQueue);
        sender.send(message);
        MessageConsumer consumer = session.createConsumer(replyQueue);
        TextMessage replyMsg = (TextMessage) consumer.receive(5000);
        log.trace("Message received:" + replyMsg);
        return replyMsg.getText();
    } finally {
        if (ic != null) {
            try {
                ic.close();
            } catch (Exception ignore) {
            }
        }
        closeConnection(connection);
    }
}
Also used : InitialContext(javax.naming.InitialContext) SessionContext(javax.ejb.SessionContext) Context(javax.naming.Context) ConnectionFactory(javax.jms.ConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) Connection(javax.jms.Connection) TemporaryQueue(javax.jms.TemporaryQueue) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TemporaryQueue(javax.jms.TemporaryQueue) TextMessage(javax.jms.TextMessage) NamingException(javax.naming.NamingException) JMSException(javax.jms.JMSException) Session(javax.jms.Session)

Example 7 with MessageProducer

use of javax.jms.MessageProducer in project wildfly by wildfly.

the class RunAsTestCaseEJBMDB method testSendMessage.

@Test
public void testSendMessage() throws Exception {
    ConnectionFactory cf = null;
    Connection connection = null;
    Session session = null;
    try {
        cf = (ConnectionFactory) initialContext.lookup("jms/RemoteConnectionFactory");
        Queue queue = (Queue) initialContext.lookup(QUEUE_NAME);
        connection = cf.createConnection("guest", "guest");
        //for consumer we need to start connection
        connection.start();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer sender = session.createProducer(queue);
        TemporaryQueue replyQueue = session.createTemporaryQueue();
        TextMessage message = session.createTextMessage("hello goodbye");
        message.setJMSReplyTo(replyQueue);
        sender.send(message);
        log.trace("testSendMessage(): Message sent!");
        MessageConsumer consumer = session.createConsumer(replyQueue);
        Message replyMsg = consumer.receive(5000);
        Assert.assertNotNull(replyMsg);
        Assert.assertTrue(replyMsg instanceof TextMessage);
        String actual = ((TextMessage) replyMsg).getText();
        Assert.assertEquals("Howdy Fred! GoodBye user1", actual);
    } finally {
        if (session != null) {
            session.close();
        }
        closeConnection(connection);
    }
}
Also used : ConnectionFactory(javax.jms.ConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) Connection(javax.jms.Connection) TemporaryQueue(javax.jms.TemporaryQueue) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TemporaryQueue(javax.jms.TemporaryQueue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 8 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 9 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 10 with MessageProducer

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

the class InOutMessageHandler method handleMessage.

@Override
public void handleMessage(final Exchange exchange) {
    try {
        MessageProducer messageProducer = null;
        Object obj = exchange.getIn().getHeader(JmsConstants.JMS_REPLY_TO);
        if (obj != null) {
            Destination replyTo;
            if (isDestination(obj)) {
                replyTo = (Destination) obj;
            } else if (obj instanceof String) {
                replyTo = getEndpoint().getDestinationCreationStrategy().createDestination(getSession(), (String) obj, isTopic());
            } else {
                throw new Exception("The value of JMSReplyTo must be a valid Destination or String.  Value provided: " + obj);
            }
            String destinationName = getDestinationName(replyTo);
            try {
                lock.readLock().lock();
                if (producerCache.containsKey(destinationName)) {
                    messageProducer = producerCache.get(destinationName);
                }
            } finally {
                lock.readLock().unlock();
            }
            if (messageProducer == null) {
                try {
                    lock.writeLock().lock();
                    messageProducer = getSession().createProducer(replyTo);
                    producerCache.put(destinationName, messageProducer);
                } finally {
                    lock.writeLock().unlock();
                }
            }
        }
        MessageHandlerAsyncCallback callback = new MessageHandlerAsyncCallback(exchange, messageProducer);
        if (exchange.isFailed()) {
            return;
        } else {
            if (isTransacted() || isSynchronous()) {
                // must process synchronous if transacted or configured to
                // do so
                log.debug("Synchronous processing: Message[{}], Destination[{}] ", exchange.getIn().getBody(), getEndpoint().getEndpointUri());
                try {
                    getProcessor().process(exchange);
                } catch (Exception e) {
                    exchange.setException(e);
                } finally {
                    callback.done(true);
                }
            } else {
                // process asynchronous using the async routing engine
                log.debug("Asynchronous processing: Message[{}], Destination[{}] ", exchange.getIn().getBody(), getEndpoint().getEndpointUri());
                getProcessor().process(exchange, callback);
            }
        }
    } catch (Exception e) {
        exchange.setException(e);
    }
    if (log.isDebugEnabled()) {
        log.debug("SjmsMessageConsumer invoked for Exchange id:{} ", exchange.getExchangeId());
    }
}
Also used : Destination(javax.jms.Destination) MessageProducer(javax.jms.MessageProducer) JMSException(javax.jms.JMSException)

Aggregations

MessageProducer (javax.jms.MessageProducer)1512 Session (javax.jms.Session)1302 MessageConsumer (javax.jms.MessageConsumer)987 Connection (javax.jms.Connection)915 TextMessage (javax.jms.TextMessage)888 Test (org.junit.Test)850 Message (javax.jms.Message)646 Queue (javax.jms.Queue)404 JMSException (javax.jms.JMSException)213 Destination (javax.jms.Destination)190 ConnectionFactory (javax.jms.ConnectionFactory)147 BytesMessage (javax.jms.BytesMessage)140 Topic (javax.jms.Topic)140 TopicSession (javax.jms.TopicSession)135 TopicConnection (javax.jms.TopicConnection)115 ActiveMQQueue (org.apache.activemq.command.ActiveMQQueue)108 ObjectMessage (javax.jms.ObjectMessage)98 CountDownLatch (java.util.concurrent.CountDownLatch)91 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)90 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)88