Search in sources :

Example 41 with MessageProducer

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

the class JmsSearchProvider method onSpellMessage.

void onSpellMessage(String spellText, String correlationID, Destination replyTo) {
    Session session = null;
    try {
        Set<String> spellStrings = this.spellCheck(spellText);
        if (spellStrings != null) {
            ArrayList<String> spellStringList = new ArrayList<String>(spellStrings);
            session = acquireSession();
            Message responseMessage = session.createObjectMessage(spellStringList);
            responseMessage.setJMSCorrelationID(correlationID);
            MessageProducer producer = session.createProducer(replyTo);
            producer.send(responseMessage);
            producer.close();
            return;
        }
        // signal search not available
        return;
    } catch (JMSException e) {
        log_.error("error when receiving jms messages", e);
        // signal search not available
        return;
    // do not throw exceptions here throw new OLATRuntimeException();
    } catch (Throwable th) {
        log_.error("error at ClusteredSearchProvider.receive()", th);
        // signal search not available
        return;
    // do not throw exceptions throw new OLATRuntimeException();
    } finally {
        releaseSession(session);
        DBFactory.getInstance().commitAndCloseSession();
    }
}
Also used : ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) ArrayList(java.util.ArrayList) JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session)

Example 42 with MessageProducer

use of javax.jms.MessageProducer in project microservice_framework by CJSCommonPlatform.

the class DefaultJmsEnvelopeSender method send.

/**
 * Sends envelope to the destination via JMS.
 *
 * @param envelope    envelope to be sent.
 * @param destination JMS destination for the envelope.
 */
@Override
public void send(final JsonEnvelope envelope, final Destination destination) {
    traceLogger.trace(LOGGER, () -> format("Sending JMS message: %s to %s", envelope, destination.toString()));
    try (Connection connection = connectionFactory.createConnection();
        Session session = connection.createSession(false, AUTO_ACKNOWLEDGE);
        MessageProducer producer = session.createProducer(destination)) {
        producer.send(envelopeConverter.toMessage(envelope, session));
    } catch (JMSException e) {
        throw new JmsEnvelopeSenderException(format("Exception while sending envelope with name %s", envelope.metadata().name()), e);
    }
    traceLogger.trace(LOGGER, () -> format("Sent JMS message: %s to %s", envelope, destination.toString()));
}
Also used : JmsEnvelopeSenderException(uk.gov.justice.services.messaging.jms.exception.JmsEnvelopeSenderException) Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session)

Example 43 with MessageProducer

use of javax.jms.MessageProducer in project core by weld.

the class SimpleMessageProducer method sendTopicMessage.

public void sendTopicMessage() {
    Connection connection = null;
    Session session = null;
    MessageProducer messageProducer = null;
    TextMessage message = null;
    try {
        connection = connectionFactory.createConnection();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        messageProducer = session.createProducer(topic);
        message = session.createTextMessage();
        message.setText(SimpleMessageProducer.class.getName());
        messageProducer.send(message);
    } catch (JMSException e) {
        throw new RuntimeException("Cannot send message", e);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 44 with MessageProducer

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

the class QpidQmf method runQuery.

/**
 * Indempotent method that connects to Qpid and uses QMF to find information about a given
 * targetType. The best reference about how to interact with QMF can be found here:
 *
 * https://access.redhat.com/documentation/en-US/
 * Red_Hat_Enterprise_MRG/2/html-single/Messaging_Programming_Reference/index.html
 *
 * Other concepts and basic docs here
 *
 * https://qpid.apache.org/releases/qpid-cpp-1.35.0/cpp-broker/book/ch02s02.html
 *
 * @param targetType
 * @param query
 * @return
 * @throws JMSException
 */
private List<Map<String, Object>> runQuery(String targetType, Map<Object, Object> query) throws JMSException {
    Session session = null;
    List<Map<String, Object>> result = new ArrayList<>();
    Connection connection = null;
    try {
        AMQAnyDestination qmfQueue = null;
        AMQAnyDestination responseQueue = null;
        try {
            qmfQueue = new AMQAnyDestination("qmf.default.direct/broker");
            responseQueue = new AMQAnyDestination("#reply-queue; {create:always, node:{x-declare:{auto-delete:true}}}");
        } catch (URISyntaxException e) {
            throw new RuntimeException("Couldn't create destinations", e);
        }
        connection = qpidConnection.newConnection();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer sender = session.createProducer(qmfQueue);
        MessageConsumer receiver = session.createConsumer(responseQueue);
        MapMessage request = session.createMapMessage();
        request.setJMSReplyTo(responseQueue);
        request.setStringProperty("x-amqp-0-10.app-id", "qmf2");
        request.setStringProperty("qmf.opcode", "_query_request");
        request.setObject(targetType, query);
        // method name to be
        request.setObject("_what", "OBJECT");
        request.setJMSType("amqp/map");
        sender.send(request);
        Message response = receiver.receive(config.getInt(ConfigProperties.QPID_QMF_RECEIVE_TIMEOUT));
        if (response != null) {
            if (response instanceof MapMessage) {
                log.debug("Result received {}", response);
                MapMessage mm = (MapMessage) response;
                Enumeration en = mm.getMapNames();
                while (en.hasMoreElements()) {
                    Map<String, Object> next = (Map<String, Object>) mm.getObject(en.nextElement().toString());
                    result.add(next);
                }
                return result;
            } else {
                log.error("Received response in incorrect format: {}", response);
            }
        } else {
            log.error("No response received");
        }
    } catch (JMSException e) {
        throw e;
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
            if (session != null) {
                session.close();
            }
        } catch (JMSException e) {
            log.warn("Error closing the Qpid connection", e);
        }
    }
    return null;
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Enumeration(java.util.Enumeration) MapMessage(javax.jms.MapMessage) Message(javax.jms.Message) MapMessage(javax.jms.MapMessage) ArrayList(java.util.ArrayList) Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) URISyntaxException(java.net.URISyntaxException) AMQAnyDestination(org.apache.qpid.client.AMQAnyDestination) MessageProducer(javax.jms.MessageProducer) Map(java.util.Map) Session(javax.jms.Session)

Example 45 with MessageProducer

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

the class RequestReplyScenariosWithCachedConsumersTests method messageCorrelationBasedOnRequestCorrelationIdTimedOutFirstReplyOptimized.

@Test
public void messageCorrelationBasedOnRequestCorrelationIdTimedOutFirstReplyOptimized() throws Exception {
    ActiveMqTestUtils.prepare();
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("producer-cached-consumers.xml", this.getClass());
    try {
        RequestReplyExchanger gateway = context.getBean("correlationPropagatingConsumerWithOptimizationDelayFirstReply", RequestReplyExchanger.class);
        final ConnectionFactory connectionFactory = context.getBean("jmsConnectionFactory", ConnectionFactory.class);
        final Destination requestDestination = context.getBean("siOutQueueE", Destination.class);
        final Destination replyDestination = context.getBean("siInQueueE", Destination.class);
        for (int i = 0; i < 3; i++) {
            try {
                gateway.exchange(gateway.exchange(new GenericMessage<String>("foo")));
            } catch (Exception e) {
            /*ignore*/
            }
        }
        final CountDownLatch latch = new CountDownLatch(1);
        new Thread(() -> {
            DefaultMessageListenerContainer dmlc = new DefaultMessageListenerContainer();
            dmlc.setConnectionFactory(connectionFactory);
            dmlc.setDestination(requestDestination);
            dmlc.setMessageListener((SessionAwareMessageListener<Message>) (message, session) -> {
                String requestPayload = (String) extractPayload(message);
                try {
                    TextMessage replyMessage = session.createTextMessage();
                    replyMessage.setText(requestPayload);
                    replyMessage.setJMSCorrelationID(message.getJMSCorrelationID());
                    MessageProducer producer = session.createProducer(replyDestination);
                    producer.send(replyMessage);
                } catch (Exception e) {
                // ignore. the test will fail
                }
            });
            dmlc.afterPropertiesSet();
            dmlc.start();
            latch.countDown();
        }).start();
        latch.await();
        TestUtils.getPropertyValue(context.getBean("fastGateway"), "handler", JmsOutboundGateway.class).setReceiveTimeout(10000);
        Thread.sleep(1000);
        assertEquals("bar", gateway.exchange(new GenericMessage<String>("bar")).getPayload());
    } finally {
        context.close();
    }
}
Also used : Destination(javax.jms.Destination) SessionAwareMessageListener(org.springframework.jms.listener.SessionAwareMessageListener) JmsOutboundGateway(org.springframework.integration.jms.JmsOutboundGateway) CountDownLatch(java.util.concurrent.CountDownLatch) MessageTimeoutException(org.springframework.integration.MessageTimeoutException) DefaultMessageListenerContainer(org.springframework.jms.listener.DefaultMessageListenerContainer) GenericMessage(org.springframework.messaging.support.GenericMessage) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) RequestReplyExchanger(org.springframework.integration.gateway.RequestReplyExchanger) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

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