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();
}
}
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()));
}
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();
}
}
}
}
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;
}
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();
}
}
Aggregations