javax.jms.Message.getJMSMessageID()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(12.0k)|赞(0)|评价(0)|浏览(279)

本文整理了Java中javax.jms.Message.getJMSMessageID()方法的一些代码示例,展示了Message.getJMSMessageID()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Message.getJMSMessageID()方法的具体详情如下:
包路径:javax.jms.Message
类名称:Message
方法名:getJMSMessageID

Message.getJMSMessageID介绍

[英]Gets the message ID.

The JMSMessageID header field contains a value that uniquely identifies each message sent by a provider.

When a message is sent, JMSMessageID can be ignored. When the send or publish method returns, it contains a provider-assigned value.

A JMSMessageID is a String value that should function as a unique key for identifying messages in a historical repository. The exact scope of uniqueness is provider-defined. It should at least cover all messages for a specific installation of a provider, where an installation is some connected set of message routers.

All JMSMessageID values must start with the prefix 'ID:'. Uniqueness of message ID values across different providers is not required.

Since message IDs take some effort to create and increase a message's size, some JMS providers may be able to optimize message overhead if they are given a hint that the message ID is not used by an application. By calling the MessageProducer.setDisableMessageID method, a JMS client enables this potential optimization for all messages sent by that message producer. If the JMS provider accepts this hint, these messages must have the message ID set to null; if the provider ignores the hint, the message ID must be set to its normal unique value.
[中]获取消息ID。
JMSMessageID标头字段包含一个值,该值唯一标识提供者发送的每条消息。
发送消息时,可以忽略JMSMessageID。当send或publish方法返回时,它包含提供程序指定的值。
JMSMessageID是一个字符串值,它应该作为标识历史存储库中消息的唯一键。唯一性的确切范围由提供者定义。它至少应该涵盖提供商特定安装的所有消息,其中安装是一组连接的消息路由器。
所有JMSMessageID值必须以前缀“ID:”开头。不需要跨不同提供程序的消息ID值的唯一性。
由于消息ID需要一些努力来创建和增加消息的大小,因此,如果给一些JMS提供程序一个消息ID未被应用程序使用的提示,它们可能能够优化消息开销。打电话给消息制作人。通过setDisableMessageID方法,JMS客户机可以对该消息生产者发送的所有消息进行这种潜在的优化。如果JMS提供程序接受此提示,则这些消息的消息ID必须设置为null;如果提供程序忽略该提示,则必须将消息ID设置为其正常的唯一值。

代码示例

代码示例来源:origin: apache/storm

@Override
public void onMessage(Message msg) {
  try {
    LOG.trace("Queuing msg [" + msg.getJMSMessageID() + "]");
  } catch (JMSException e) {
    // Nothing here, could not get message id
  }
  this.queue.offer(msg);
}

代码示例来源:origin: apache/storm

/**
 * Fail a batch with the given transaction id. This is called when a batch is timed out, or a new batch with a 
 * matching transaction id is emitted. Note that the current implementation does nothing - i.e. it discards 
 * messages that have been failed.
 * @param transactionId The transaction id of the failed batch
 * @param messages The list of messages to fail.
 */
private void fail(Long transactionId, List<Message> messages) {
  LOG.debug("Failure for batch with transaction id " + transactionId + " for " + name);
  if (messages != null) {
    for (Message msg : messages) {
      try {
        LOG.trace("Failed message " + msg.getJMSMessageID());
      } catch (JMSException e) {
        LOG.warn("Could not identify failed message ", e);
      }
    }
  } else {
    LOG.warn("Failed batch has no messages with transaction id " + transactionId);
  }
}

代码示例来源:origin: apache/activemq

/**
 * Checks if this message has been seen before
 *
 * @param message
 * @return true if the message is a duplicate
 * @throws JMSException
 */
public boolean isDuplicate(Message message) throws JMSException {
  return isDuplicate(message.getJMSMessageID());
}

代码示例来源:origin: apache/activemq

/**
 * Check the message is in order
 *
 * @param msg
 *
 * @return true if the id is in order
 *
 * @throws JMSException
 */
public boolean isInOrder(Message msg) throws JMSException {
  return isInOrder(msg.getJMSMessageID());
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Post-process the given response message before it will be sent.
 * <p>The default implementation sets the response's correlation id
 * to the request message's correlation id, if any; otherwise to the
 * request message id.
 * @param request the original incoming JMS message
 * @param response the outgoing JMS message about to be sent
 * @throws JMSException if thrown by JMS API methods
 * @see javax.jms.Message#setJMSCorrelationID
 */
protected void postProcessResponse(Message request, Message response) throws JMSException {
  String correlation = request.getJMSCorrelationID();
  if (correlation == null) {
    correlation = request.getJMSMessageID();
  }
  response.setJMSCorrelationID(correlation);
}

代码示例来源:origin: apache/storm

@Override
void emit(final Message msg) {
  LOG.debug("Received msg {}, Requesting acks.", msg);
  try {
    JmsMessageID messageId = new JmsMessageID(messageSequence++,
      msg.getJMSMessageID());
    Values vals = tupleProducer.toTuple(msg);
    collector.emit(vals, messageId);
    pendingAcks.put(messageId, msg);
  } catch (JMSException ex) {
    LOG.warn("Error processing message {}", msg);
  }
}

代码示例来源:origin: spring-projects/spring-framework

throw new MessageConversionException(
    "Could not find type id property [" + this.typeIdPropertyName + "] on message [" +
    message.getJMSMessageID() + "] from destination [" + message.getJMSDestination() + "]");

代码示例来源:origin: apache/activemq

private void sendMessage(MessageProducer producer, String threadName) throws Exception {
  Message message = createMessage(sentCount.get());
  producer.send(message);
  if (LOG.isDebugEnabled()) {
    LOG.debug(threadName + " Sent: " + (message instanceof TextMessage ? ((TextMessage) message).getText() : message.getJMSMessageID()));
  }
  if (transactionBatchSize > 0 && sentCount.get() > 0 && sentCount.get() % transactionBatchSize == 0) {
    LOG.info(threadName + " Committing transaction: " + transactions++);
    session.commit();
  }
  if (sleep > 0) {
    Thread.sleep(sleep);
  }
}

代码示例来源:origin: spring-projects/spring-batch

/**
 * If the message is a {@link Message} then returns the JMS message ID.
 * Otherwise just return the first argument.
 * 
 * @see org.springframework.retry.interceptor.MethodArgumentsKeyGenerator#getKey(Object[])
 * 
 * @throws UnexpectedInputException if the JMS id cannot be determined from
 * a JMS Message
 * @throws IllegalArgumentException if the arguments are empty
 */
@Override
public Object getKey(Object[] items) {
  for (Object item : items) {
    if (item instanceof Message) {
      try {
        return ((Message) item).getJMSMessageID();
      }
      catch (JMSException e) {
        throw new UnexpectedInputException("Could not extract message ID", e);
      }
    }
  }
  if (items.length == 0) {
    throw new IllegalArgumentException(
        "Method parameters are empty.  The key generator cannot determine a unique key.");
  }
  return items[0];
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Create the invocation result response message.
 * <p>The default implementation creates a JMS ObjectMessage for the given
 * RemoteInvocationResult object. It sets the response's correlation id
 * to the request message's correlation id, if any; otherwise to the
 * request message id.
 * @param request the original request message
 * @param session the JMS session to use
 * @param result the invocation result
 * @return the message response to send
 * @throws javax.jms.JMSException if creating the message failed
 */
protected Message createResponseMessage(Message request, Session session, RemoteInvocationResult result)
    throws JMSException {
  Message response = this.messageConverter.toMessage(result, session);
  String correlation = request.getJMSCorrelationID();
  if (correlation == null) {
    correlation = request.getJMSMessageID();
  }
  response.setJMSCorrelationID(correlation);
  return response;
}

代码示例来源:origin: log4j/log4j

sbuf.append(m.getJMSMessageID());

代码示例来源:origin: spring-projects/spring-framework

String messageId = jmsMessage.getJMSMessageID();
if (messageId != null) {
  headers.put(JmsHeaders.MESSAGE_ID, messageId);

代码示例来源:origin: apache/storm

@Override
public void success(TransactionAttempt tx) {
  @SuppressWarnings("unchecked")
  List<Message> messages = (List<Message>) batchMessageMap.remove(tx.getTransactionId());
  if (messages != null) {
    if (!messages.isEmpty()) {
      LOG.debug("Success for batch with transaction id " + tx.getTransactionId() + "/" + tx.getAttemptId() + " for " + name);
    }
    for (Message msg : messages) {
      String messageId = "UnknownId";
      try {
        messageId = msg.getJMSMessageID();
        msg.acknowledge();
        LOG.trace("Acknowledged message " + messageId);
      } catch (JMSException e) {
        LOG.warn("Failed to acknowledge message " + messageId, e);
      }
    }
  } else {
    LOG.warn("No messages found in batch with transaction id " + tx.getTransactionId() + "/" + tx.getAttemptId());
  }
}

代码示例来源:origin: apache/nifi

private Map<String, String> extractMessageHeaders(final Message message) throws JMSException {
  final Map<String, String> messageHeaders = new HashMap<>();
  messageHeaders.put(JmsHeaders.DELIVERY_MODE, String.valueOf(message.getJMSDeliveryMode()));
  messageHeaders.put(JmsHeaders.EXPIRATION, String.valueOf(message.getJMSExpiration()));
  messageHeaders.put(JmsHeaders.PRIORITY, String.valueOf(message.getJMSPriority()));
  messageHeaders.put(JmsHeaders.REDELIVERED, String.valueOf(message.getJMSRedelivered()));
  messageHeaders.put(JmsHeaders.TIMESTAMP, String.valueOf(message.getJMSTimestamp()));
  messageHeaders.put(JmsHeaders.CORRELATION_ID, message.getJMSCorrelationID());
  messageHeaders.put(JmsHeaders.MESSAGE_ID, message.getJMSMessageID());
  messageHeaders.put(JmsHeaders.TYPE, message.getJMSType());
  String replyToDestinationName = this.retrieveDestinationName(message.getJMSReplyTo(), JmsHeaders.REPLY_TO);
  if (replyToDestinationName != null) {
    messageHeaders.put(JmsHeaders.REPLY_TO, replyToDestinationName);
  }
  String destinationName = this.retrieveDestinationName(message.getJMSDestination(), JmsHeaders.DESTINATION);
  if (destinationName != null) {
    messageHeaders.put(JmsHeaders.DESTINATION, destinationName);
  }
  return messageHeaders;
}

代码示例来源:origin: apache/rocketmq-externals

@Override
public void onMessage(Message message) {
  try {
    Assert.assertNotNull(message);
    Assert.assertNotNull(message.getJMSMessageID());
    if (consumedNum.incrementAndGet() == expectd && latch != null) {
      latch.countDown();
    }
  }
  catch (Exception e) {
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: apache/rocketmq-externals

@Override
  public void onMessage(Message message) {
    try {
      Assert.assertNotNull(message);
      Assert.assertNotNull(message.getJMSMessageID());
    }
    catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
};

代码示例来源:origin: spring-projects/spring-batch

@Test
public void testGetKeyFromMessage() throws Exception {
  Message message = mock(Message.class);
  when(message.getJMSMessageID()).thenReturn("foo");
  JmsItemReader<Message> itemReader = new JmsItemReader<>();
  itemReader.setItemType(Message.class);
  assertEquals("foo", methodArgumentsKeyGenerator.getKey(new Object[]{message}));
}

代码示例来源:origin: spring-projects/spring-batch

retryTemplate.execute(callback, recoveryCallback, new DefaultRetryState(msg.getJMSMessageID()));

代码示例来源:origin: apache/activemq

/**
   * Copies the standard JMS and user defined properties from the givem
   * message to the specified message
   *
   * @param fromMessage the message to take the properties from
   * @param toMessage the message to add the properties to
   * @throws JMSException
   */
  public static void copyProperties(Message fromMessage, Message toMessage) throws JMSException {
    toMessage.setJMSMessageID(fromMessage.getJMSMessageID());
    toMessage.setJMSCorrelationID(fromMessage.getJMSCorrelationID());
    toMessage.setJMSReplyTo(transformDestination(fromMessage.getJMSReplyTo()));
    toMessage.setJMSDestination(transformDestination(fromMessage.getJMSDestination()));
    toMessage.setJMSDeliveryMode(fromMessage.getJMSDeliveryMode());
    toMessage.setJMSRedelivered(fromMessage.getJMSRedelivered());
    toMessage.setJMSType(fromMessage.getJMSType());
    toMessage.setJMSExpiration(fromMessage.getJMSExpiration());
    toMessage.setJMSPriority(fromMessage.getJMSPriority());
    toMessage.setJMSTimestamp(fromMessage.getJMSTimestamp());

    Enumeration propertyNames = fromMessage.getPropertyNames();

    while (propertyNames.hasMoreElements()) {
      String name = propertyNames.nextElement().toString();
      Object obj = fromMessage.getObjectProperty(name);
      toMessage.setObjectProperty(name, obj);
    }
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void jmsReadOnlyPropertiesNotMapped() throws JMSException {
  Message<String> message = initBuilder()
      .setHeader(JmsHeaders.DESTINATION, new Destination() {})
      .setHeader(JmsHeaders.DELIVERY_MODE, DeliveryMode.NON_PERSISTENT)
      .setHeader(JmsHeaders.EXPIRATION, 1000L)
      .setHeader(JmsHeaders.MESSAGE_ID, "abc-123")
      .setHeader(JmsHeaders.PRIORITY, 9)
      .setHeader(JmsHeaders.REDELIVERED, true)
      .setHeader(JmsHeaders.TIMESTAMP, System.currentTimeMillis())
      .build();
  javax.jms.Message jmsMessage = new StubTextMessage();
  mapper.fromHeaders(message.getHeaders(), jmsMessage);
  assertNull(jmsMessage.getJMSDestination());
  assertEquals(DeliveryMode.PERSISTENT, jmsMessage.getJMSDeliveryMode());
  assertEquals(0, jmsMessage.getJMSExpiration());
  assertNull(jmsMessage.getJMSMessageID());
  assertEquals(javax.jms.Message.DEFAULT_PRIORITY, jmsMessage.getJMSPriority());
  assertFalse(jmsMessage.getJMSRedelivered());
  assertEquals(0, jmsMessage.getJMSTimestamp());
}

相关文章