本文整理匯總了Java中javax.jms.TextMessage.setJMSCorrelationID方法的典型用法代碼示例。如果您正苦於以下問題:Java TextMessage.setJMSCorrelationID方法的具體用法?Java TextMessage.setJMSCorrelationID怎麽用?Java TextMessage.setJMSCorrelationID使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.jms.TextMessage
的用法示例。
在下文中一共展示了TextMessage.setJMSCorrelationID方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: receiveAndRespondWithMessageIdAsCorrelationId
import javax.jms.TextMessage; //導入方法依賴的package包/類
public void receiveAndRespondWithMessageIdAsCorrelationId(ConnectionFactory connectionFactory, String queueName) throws JMSException {
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(session.createQueue(queueName));
final javax.jms.Message inMessage = consumer.receive();
String requestMessageId = inMessage.getJMSMessageID();
LOG.debug("Received message " + requestMessageId);
final TextMessage replyMessage = session.createTextMessage("Result");
replyMessage.setJMSCorrelationID(inMessage.getJMSMessageID());
final MessageProducer producer = session.createProducer(inMessage.getJMSReplyTo());
LOG.debug("Sending reply to " + inMessage.getJMSReplyTo());
producer.send(replyMessage);
producer.close();
consumer.close();
session.close();
connection.close();
}
示例2: handleMessage
import javax.jms.TextMessage; //導入方法依賴的package包/類
private void handleMessage(MessageHandler handler, Message message, Session session) {
if (! (message instanceof TextMessage)) {
return;
}
TextMessage textMessage = (TextMessage) message;
String text = null;
String requestId = UUID.randomUUID().toString();
try {
text = textMessage.getText();
if (StringUtils.isNotEmpty(message.getJMSCorrelationID())) {
requestId = message.getJMSCorrelationID();
}
MDC.put(X_OBOS_REQUEST_ID, requestId);
log.info("Received message '{}'", text);
handler.handle(new ObjectMapper().readTree(text));
} catch (Exception e) {
log.error("Failed to process message", e);
try {
TextMessage errorMessage = session.createTextMessage(text);
errorMessage.setJMSCorrelationID(requestId);
Queue queue = session.createQueue(queueError);
MessageProducer errorProducer = session.createProducer(queue);
errorProducer.send(errorMessage);
} catch (JMSException jmse) {
log.error("Failed to create error message", jmse);
}
} finally {
MDC.remove(X_OBOS_REQUEST_ID);
}
}
示例3: wrapAndSendResponseInternal
import javax.jms.TextMessage; //導入方法依賴的package包/類
private void wrapAndSendResponseInternal(Message incoming, List<B> response) throws JMSException {
String text;
try {
text = JsonMapper.serialize(response);
} catch (JsonProcessingException e) {
getLogger().warn("Failed to convert response to text. Will not send response");
return;
}
getLogger().debug("Response (object): {}", response);
getLogger().debug("Response (string): {}", text);
TextMessage msg = getContext().createTextMessage(text);
msg.setJMSCorrelationID(incoming.getJMSCorrelationID());
getContext().createProducer()
.setDisableMessageID(true)
.setDisableMessageTimestamp(true)
.setDeliveryMode(DeliveryMode.NON_PERSISTENT)
.send(incoming.getJMSReplyTo(), msg);
}
示例4: publishMessage
import javax.jms.TextMessage; //導入方法依賴的package包/類
/**
* Publish message.
*
* @param waitTaskName the wait task name
* @param woType the wo type
* @throws JMSException the jMS exception
*/
public void publishMessage(String processId, String execId, CmsWorkOrderSimpleBase wo, String waitTaskName, String woType) throws JMSException {
SimpleDateFormat format = new SimpleDateFormat(CmsConstants.SEARCH_TS_PATTERN);
wo.getSearchTags().put(CmsConstants.REQUEST_ENQUE_TS, format.format(new Date()));
//guarantee non empty-value for searchMap
if (version != null && StringUtils.isNotBlank(version.getGitVersion())) {
wo.getSearchTags().put(CONTROLLLER_VERSION_SEARCH_TAG, version.getGitVersion());
} else {
wo.getSearchTags().put(CONTROLLLER_VERSION_SEARCH_TAG, DEFAULT_VERSION);
}
TextMessage message = session.createTextMessage(gson.toJson(wo));
String corelationId = processId + "!" + execId + "!" + waitTaskName+"!"+getCtxtId(wo);
message.setJMSCorrelationID(corelationId);
message.setStringProperty("task_id", corelationId);
message.setStringProperty("type", woType);
String queueName;
if ("true".equals(System.getProperty(USE_SHARED_FLAG))) {
queueName = SHARED_QUEUE;
} else {
queueName = (wo.getCloud().getCiAttributes().get("location").replaceAll("/", ".") + QUEUE_SUFFIX).substring(
1);
}
bindingQueusMap.computeIfAbsent(queueName, k -> {
try {
return newMessageProducer(k);
} catch (JMSException e) {
throw new RuntimeException(e);
}
}).send(message);
if (logger.isDebugEnabled()) {
logger.debug("Published: " + message.getText());
}
logger.info("Posted message with id "+ corelationId +" to q: "+queueName);
}
示例5: queueMessage
import javax.jms.TextMessage; //導入方法依賴的package包/類
static void queueMessage(Session session, String text, String queueName) {
try {
Queue queue = session.createQueue(queueName);
MessageProducer producer = session.createProducer(queue);
TextMessage message = session.createTextMessage(text);
message.setJMSCorrelationID(MDC.get(X_OBOS_REQUEST_ID));
producer.send(message);
session.commit();
} catch (JMSException ex) {
throw new MessageQueueException("Could not queue message '" + text + "'", ex);
}
}
示例6: requeueFailedMessages
import javax.jms.TextMessage; //導入方法依賴的package包/類
@Override
public void requeueFailedMessages() {
try {
ActiveMQConnection connection = ActiveMqUtils.openConnection(user, password, url);
Session session = ActiveMqUtils.startSession(connection);
int count = getQueueSize(session, queueError);
if (count < 1) {
return;
}
log.info("Requeuing {} failed messages...", count);
Queue queueErr = session.createQueue(queueError);
MessageConsumer consumer = session.createConsumer(queueErr);
Queue queueRetry = session.createQueue(queueInput);
MessageProducer producer = session.createProducer(queueRetry);
for (int consumed = 0; consumed < count; consumed++) {
TextMessage message = (TextMessage) consumer.receive(REQUEUE_TIMEOUT);
if (message == null) {
continue;
}
String text = message.getText();
String requestId = message.getJMSCorrelationID();
log.info("Requeuing message '{}'", text);
try {
TextMessage newMessage = session.createTextMessage(text);
newMessage.setJMSCorrelationID(requestId);
producer.send(newMessage);
} catch (Exception e) {
log.error("Failed to requeue message", e);
}
message.acknowledge();
session.commit();
}
producer.close();
consumer.close();
} catch (JMSException ex) {
throw new MessageQueueException("Failed to requeue failed messages", ex);
}
}
示例7: testConsumerWithBasicRecover
import javax.jms.TextMessage; //導入方法依賴的package包/類
@Parameters({ "broker-port", "admin-username", "admin-password", "broker-hostname" })
@Test
public void testConsumerWithBasicRecover(String port,
String adminUsername,
String adminPassword,
String brokerHostname) throws Exception {
String queueName = "testConsumerWithBasicRecover";
InitialContext initialContextForQueue = ClientHelper
.getInitialContextBuilder(adminUsername, adminPassword, brokerHostname, port)
.withQueue(queueName)
.build();
ConnectionFactory connectionFactory
= (ConnectionFactory) initialContextForQueue.lookup(ClientHelper.CONNECTION_FACTORY);
Connection connection = connectionFactory.createConnection();
connection.start();
// publish message
Session producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = producerSession.createQueue(queueName);
MessageProducer producer = producerSession.createProducer(queue);
TextMessage firstMessage = producerSession.createTextMessage("First message for reject test");
TextMessage secondMessage = producerSession.createTextMessage("Second message for reject test");
String correlationIdOne = "1";
String correlationIdTwo = "2";
firstMessage.setJMSCorrelationID(correlationIdOne);
secondMessage.setJMSCorrelationID(correlationIdTwo);
producer.send(firstMessage);
producer.send(secondMessage);
producerSession.close();
// Consume published messages
Session subscriberSession = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
Destination subscriberDestination = (Destination) initialContextForQueue.lookup(queueName);
MessageConsumer consumer = subscriberSession.createConsumer(subscriberDestination);
Message message = consumer.receive(5000);
Assert.assertNotNull(message, "Message was not received");
subscriberSession.recover();
// Message order can change after recovering
for (int i = 0; i < 2; i++) {
message = consumer.receive(5000);
Assert.assertNotNull(message, "Requeued message was not received");
if (correlationIdOne.equals(message.getJMSCorrelationID())) {
Assert.assertTrue(message.getJMSRedelivered(), "Redelivered flag was set in second message" + message);
} else {
Assert.assertFalse(message.getJMSRedelivered(), "Redelivered flag was set in first message");
}
message.acknowledge();
}
connection.close();
}