2016-09-21 64 views
1

Iam正在處理activeMQ,其中iam通過隊列向生產者發送消息給消費者。假設我的消費者由於某種原因而失敗。 我已將消息的到期時間設置爲15分鐘,以便生產者的消息停留在隊列中,並在15分鐘後過期。 我如何編程方式檢查&最終得到通知,如果activeMQ消息已過期或不在排隊在生產者只有不在消費者(這是由於我的設計)。如何檢查消息是否已過期或在生產者中的activemq中

代碼片段如下。

初始化代碼

public void init() { 
     try { 
      final LocalProperties config = new LocalProperties(); 
      final ConnectionFactory factory = new ActiveMQConnectionFactory(config.getActiveMqConnection()); 
      this.connection = factory.createConnection(config.getActiveMqUser(), config.getActiveMqPassword()); 
      this.connection.start(); 
      this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 
      final Destination destination = this.session.createQueue(QUEUE_NAME); 
      this.producer = this.session.createProducer(destination); 
      this.producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); 
     } catch (final JMSException e) { 
      _LOG.error("We could not open the active mq connection", e); 
     } 
    } 

生產者代碼

public Response PostMessages(
      final MessageDelivery body, final String messageId) { 

     try { 
      this.openConnection(); 
      // Create a messages 
      final Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create(); 
      final String json = gson.toJson(body); 
      final TextMessage request = this.session.createTextMessage(json); 
      request.setLongProperty(
        "expiryTime", 
        900000 
        ); 
      request.setStringProperty("messageType", "com/messages/post"); 
      request.setBooleanProperty("deliveryNotification", false); 
      request.setStringProperty("destination", "XXXX"); 
      request.setStringProperty("destinationType", "FILTER"); 
      _LOG.info(request.getText()); 
      // Tell the producer to send the message 
      producer.send(request); 

      //How to get the expired message after producer.send(request) 

      return Response.status(Response.Status.ACCEPTED).build(); 
     } catch (final Exception e) { 
      _LOG.error("We could not send the message", e); 
      return Response.status(Response.Status.BAD_REQUEST).build(); 
     } 
    }* 

回答

1

默認情況下,如果消息是過期並且所述消息是不是持久的,將被丟棄,並且該應用程序將不會被能夠得到它。如果是在AMQ你有一個永久設置它將被移至DLQ

詳情here:在使用隊列瀏覽Automatically Discard Expired Messages

+0

有沒有什麼方法可行,因爲我們可以訪問message.getJMSexpiration() –

+0

機會是你的應用程序甚至不會獲得訪問屬性的消息(如果消息已過期)。您可能需要從DLQ重新處理它,如果這是可能的 –

+0

我讀** ** ActiveMQ.Advisory.Expired.Queue **,但無法獲得任何代碼示例實現。我想監視生產者隊列以檢查過期的消息。如果你可以提供一個代碼示例,這將是很大的 –