Java Code Examples for javax.jms.Message#getBooleanProperty()

The following examples show how to use javax.jms.Message#getBooleanProperty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DurableSubscriptionOffline3Test.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Override
public void onMessage(Message message) {
   count++;

   try {
      Object b = message.getObjectProperty("$b");
      if (b != null) {
         boolean c = message.getBooleanProperty("$c");
         assertTrue("", c);
      } else {
         String d = message.getStringProperty("$d");
         assertTrue("", "D1".equals(d) || "D2".equals(d));
      }
   } catch (JMSException e) {
      e.printStackTrace();
      exceptions.add(e);
   }
}
 
Example 2
Source File: MQTemplate.java    From Thunder with Apache License 2.0 5 votes vote down vote up
@Override
protected void doSend(Session session, Destination destination, MessageCreator messageCreator) throws JMSException {
    MessageProducer producer = null;
    try {
        Message message = messageCreator.createMessage(session);
        boolean async = message.getBooleanProperty(ThunderConstant.ASYNC_ATTRIBUTE_NAME);
        long timeout = message.getLongProperty(ThunderConstant.TIMEOUT_ATTRIBUTE_NAME);

        producer = createProducer(session, destination);
        // DeliveryMode.PERSISTENT:持久化模式,消息在硬盘堆积模式
        // DeliveryMode.NON_PERSISTENT:非持久化模式,消息在内存堆积模式
        if (async) {
            producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        } else {
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        }
        producer.setTimeToLive(timeout);

        doSend(producer, message);

        if (session.getTransacted() && isSessionLocallyTransacted(session)) {
            JmsUtils.commitIfNecessary(session);
        }
    } finally {
        if (producer != null) {
            JmsUtils.closeMessageProducer(producer);
        }
    }
}
 
Example 3
Source File: Jms.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
public static boolean isHttpMessage(Message message) {
   try {
      return message.getBooleanProperty(HttpMessageHelper.POSTED_AS_HTTP_MESSAGE);
   } catch (JMSException e) {
      return false;
   }
}
 
Example 4
Source File: MessageGroupNewConsumerTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
public String formatMessage(Message m) {
   try {
      return m.getStringProperty("JMSXGroupID") + "-" + m.getIntProperty("JMSXGroupSeq") + "-" + m.getBooleanProperty("JMSXGroupFirstForConsumer");
   } catch (Exception e) {
      return e.getClass().getSimpleName() + ": " + e.getMessage();
   }
}
 
Example 5
Source File: MessageGroupCloseTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
public void checkMessage(Message m,
                         String consumerId,
                         Map<String, Integer> messageGroups,
                         Set<String> closedGroups) throws JMSException {
   String groupId = m.getStringProperty("JMSXGroupID");
   int seq = m.getIntProperty("JMSXGroupSeq");
   Integer count = messageGroups.get(groupId);
   if (count == null) {
      // first time seeing this group
      if (!m.propertyExists("JMSXGroupFirstForConsumer") || !m.getBooleanProperty("JMSXGroupFirstForConsumer")) {
         LOG.info(consumerId + ": JMSXGroupFirstForConsumer not set for group=" + groupId + ", seq=" + seq);
         errorCountFirstForConsumer++;
      }
      if (seq == -1) {
         closedGroups.add(groupId);
         LOG.info(consumerId + ": wrong consumer got close message for group=" + groupId);
         errorCountWrongConsumerClose++;
      }
      messageGroups.put(groupId, 1);
   } else {
      // existing group
      if (closedGroups.contains(groupId)) {
         // group reassigned to same consumer
         closedGroups.remove(groupId);
         if (!m.propertyExists("JMSXGroupFirstForConsumer") || !m.getBooleanProperty("JMSXGroupFirstForConsumer")) {
            LOG.info(consumerId + ": JMSXGroupFirstForConsumer not set for group=" + groupId + ", seq=" + seq);
            errorCountFirstForConsumer++;
         }
         if (seq == -1) {
            LOG.info(consumerId + ": consumer got duplicate close message for group=" + groupId);
            errorCountDuplicateClose++;
         }
      }
      if (seq == -1) {
         closedGroups.add(groupId);
      }
      messageGroups.put(groupId, count + 1);
   }
}
 
Example 6
Source File: AutoAckMessageListenerTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
public void onMessage(Message message) {
   try {
      if (message.getBooleanProperty("last") == false) {
         log.debug("Received first message.");
         if (message.getJMSRedelivered() == true) {
            // should not re-receive this one
            log.debug("Error: received first message twice");
            passed = false;
         }
      } else {
         if (message.getJMSRedelivered() == false) {
            // received second message for first time
            log.debug("Received second message. Calling recover()");
            session.recover();
         } else {
            // should be redelivered after recover
            log.debug("Received second message again as expected");
            passed = true;
            monitor.countDown();
         }
      }
   } catch (JMSException e) {
      log.warn("Exception caught in message listener:\n" + e);
      passed = false;
      monitor.countDown();
   }

}
 
Example 7
Source File: CoordinatorClient.java    From hazelcast-simulator with Apache License 2.0 5 votes vote down vote up
private boolean processResponses(RemoteBroker remoteBroker) {
    try {
        Message replyMessage = remoteBroker.replyQueueConsumer.receiveNoWait();
        if (replyMessage == null) {
            return false;
        }

        String correlationId = replyMessage.getJMSCorrelationID();
        FutureImpl future = futures.remove(correlationId);
        if (future == null) {
            LOGGER.debug("No future for " + correlationId + "\n" + replyMessage);
        } else {
            boolean error = replyMessage.getBooleanProperty("error");
            if (error) {
                String message = replyMessage.getStringProperty("message");
                future.complete(new Exception(message));
            } else {
                future.complete(replyMessage.getStringProperty("payload"));
            }
        }
        return true;
    } catch (Exception e) {
        if (!stop) {
            //todo: feed into failure collector
            LOGGER.fatal(e);
        }
        return false;
    }
}
 
Example 8
Source File: BasicRequestor.java    From solace-samples-jms with Apache License 2.0 4 votes vote down vote up
public void run(String... args) throws Exception {

        String[] split = args[1].split("@");

        String host = args[0];
        String vpnName = split[1];
        String username = split[0];
        String password = args[2];

        System.out.printf("BasicRequestor is connecting to Solace messaging at %s...%n", host);

        // Programmatically create the connection factory using default settings
        SolConnectionFactory connectionFactory = SolJmsUtility.createConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setVPN(vpnName);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);

        // Create connection to the Solace router
        Connection connection = connectionFactory.createConnection();

        // Create a non-transacted, auto ACK session.
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        System.out.printf("Connected to the Solace Message VPN '%s' with client username '%s'.%n", vpnName,
                username);

        // Create the request topic programmatically
        Topic requestTopic = session.createTopic(REQUEST_TOPIC_NAME);

        // Create the message producer for the created queue
        MessageProducer requestProducer = session.createProducer(requestTopic);

        // The response will be received on this temporary queue.
        TemporaryQueue replyToQueue = session.createTemporaryQueue();

        // Create consumer for receiving the request's reply
        MessageConsumer replyConsumer = session.createConsumer(replyToQueue);

        // Start receiving replies
        connection.start();

        // Create a request.
        TextMessage request = session.createTextMessage("Sample Request");
        // The application must put the destination of the reply in the replyTo field of the request
        request.setJMSReplyTo(replyToQueue);
        // The application must put a correlation ID in the request
        String correlationId = UUID.randomUUID().toString();
        request.setJMSCorrelationID(correlationId);

        System.out.printf("Sending request '%s' to topic '%s'...%n", request.getText(), requestTopic.toString());

        // Send the request
        requestProducer.send(requestTopic, request, DeliveryMode.NON_PERSISTENT,
                Message.DEFAULT_PRIORITY,
                Message.DEFAULT_TIME_TO_LIVE);

        System.out.println("Sent successfully. Waiting for reply...");

        // the main thread blocks at the next statement until a message received or the timeout occurs
        Message reply = replyConsumer.receive(REPLY_TIMEOUT_MS);

        if (reply == null) {
            throw new Exception("Failed to receive a reply in " + REPLY_TIMEOUT_MS + " msecs");
        }

        // Process the reply
        if (reply.getJMSCorrelationID() == null) {
            throw new Exception(
                    "Received a reply message with no correlationID. This field is needed for a direct request.");
        }

        // Apache Qpid JMS prefixes correlation ID with string "ID:" so remove such prefix for interoperability
        if (!reply.getJMSCorrelationID().replaceAll("ID:", "").equals(correlationId)) {
            throw new Exception("Received invalid correlationID in reply message.");
        }

        if (reply instanceof TextMessage) {
            System.out.printf("TextMessage response received: '%s'%n", ((TextMessage) reply).getText());
            if (!reply.getBooleanProperty(SupportedProperty.SOLACE_JMS_PROP_IS_REPLY_MESSAGE)) {
                System.out.println("Warning: Received a reply message without the isReplyMsg flag set.");
            }
        } else {
            System.out.println("Message response received.");
        }

        System.out.printf("Message Content:%n%s%n", SolJmsUtility.dumpMessage(reply));

        connection.stop();
        // Close everything in the order reversed from the opening order
        // NOTE: as the interfaces below extend AutoCloseable,
        // with them it's possible to use the "try-with-resources" Java statement
        // see details at https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
        replyConsumer.close();
        requestProducer.close();
        session.close();
        connection.close();
    }
 
Example 9
Source File: MessageGroupLateArrivalsTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Override
public void run() {

   try {
      startSignal.await();
      log.info(workerName);
      Session sess = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
      MessageConsumer consumer = sess.createConsumer(queueName);
      workerStarted.countDown();

      while (true) {
         if (counters[0] == 0 && counters[1] == 0 && counters[2] == 0) {
            doneSignal.countDown();
            log.info(workerName + " done...");
            break;
         }

         Message msg = consumer.receive(500);
         if (msg == null)
            continue;

         msg.acknowledge();

         String group = msg.getStringProperty("JMSXGroupID");
         msg.getBooleanProperty("JMSXGroupFirstForConsumer");

         if ("A".equals(group)) {
            --counters[0];
            update(group);
         } else if ("B".equals(group)) {
            --counters[1];
            update(group);
         } else if ("C".equals(group)) {
            --counters[2];
            update(group);
         } else {
            log.warn(workerName + ", unknown group");
         }
         if (counters[0] != 0 || counters[1] != 0 || counters[2] != 0) {
            msg.acknowledge();
         }
      }
      consumer.close();
      sess.close();
   } catch (Exception e) {
      e.printStackTrace();
   }
}
 
Example 10
Source File: MessageGroupDelayedTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Override
public void run() {

   try {
      log.info(workerName);
      startSignal.await();
      Session sess = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
      MessageConsumer consumer = sess.createConsumer(queueName);

      while (true) {
         if (counters[0] == 0 && counters[1] == 0 && counters[2] == 0) {
            doneSignal.countDown();
            log.info(workerName + " done...");
            break;
         }

         Message msg = consumer.receive(500);
         if (msg == null)
            continue;

         String group = msg.getStringProperty("JMSXGroupID");
         msg.getBooleanProperty("JMSXGroupFirstForConsumer");

         if ("A".equals(group)) {
            --counters[0];
            update(group);
            Thread.sleep(500);
         } else if ("B".equals(group)) {
            --counters[1];
            update(group);
            Thread.sleep(100);
         } else if ("C".equals(group)) {
            --counters[2];
            update(group);
            Thread.sleep(10);
         } else {
            log.warn("unknown group");
         }
         if (counters[0] != 0 || counters[1] != 0 || counters[2] != 0) {
            msg.acknowledge();
         }
      }
      consumer.close();
      sess.close();
   } catch (Exception e) {
      e.printStackTrace();
   }
}