コード例 #1
0
 /**
  * @param type
  * @param clazz
  * @param bool
  * @throws JMSException
  */
 private void bodyAssignableFrom(final JmsMessageType type, final boolean bool, Class... clazz)
     throws JMSException {
   Assert.assertNotNull("clazz!=null", clazz);
   Assert.assertTrue("clazz[] not empty", clazz.length > 0);
   Object body = createBodySendAndReceive(type);
   Message msg = queueConsumer.receive(500);
   Assert.assertNotNull("must have a msg", msg);
   Assert.assertEquals(type.toString(), msg.getStringProperty("type"));
   for (Class<?> c : clazz) {
     Assert.assertEquals(
         msg + " " + type + " & " + c + ": " + bool, bool, msg.isBodyAssignableTo(c));
     if (bool) {
       Object receivedBody = msg.getBody(c);
       Assert.assertTrue("correct type " + c, c.isInstance(receivedBody));
       if (body.getClass().isAssignableFrom(byte[].class)) {
         Arrays.equals((byte[]) body, (byte[]) receivedBody);
       } else {
         Assert.assertEquals(
             "clazz=" + c + ", bodies must match.. " + body.equals(receivedBody),
             body,
             receivedBody);
       }
     } else {
       try {
         Object foo = msg.getBody(c);
         Assert.fail("expected a " + MessageFormatException.class);
       } catch (MessageFormatException e) {
         // expected
       }
     }
   }
 }
コード例 #2
0
 @Override
 public <T> T getBody(Class<T> c) throws JMSException {
   if (ActiveMQRAMessage.trace) {
     ActiveMQRALogger.LOGGER.trace("getBody(" + c + ")");
   }
   return message.getBody(c);
 }
コード例 #3
0
 public String readMessage() throws JMSException {
   JMSConsumer jmsConsumer = jmsContext.createConsumer(queue);
   Message message = jmsConsumer.receive(5000);
   return message.getBody(String.class);
 }
コード例 #4
0
  /**
   * Main method.
   *
   * @param args the destination name and type used by the example
   */
  public static void main(String[] args) {
    String destType;
    Destination dest = null;
    JMSConsumer consumer;

    if (args.length != 1) {
      System.err.println("Program takes one argument: <dest_type>");
      System.exit(1);
    }

    destType = args[0];
    System.out.println("Destination type is " + destType);

    if (!(destType.equals("queue") || destType.equals("topic"))) {
      System.err.println("Argument must be \"queue\" or \"topic\"");
      System.exit(1);
    }

    try {
      if (destType.equals("queue")) {
        dest = (Destination) queue;
      } else {
        dest = (Destination) topic;
      }
    } catch (JMSRuntimeException e) {
      System.err.println("Error setting destination: " + e.toString());
      System.exit(1);
    }

    /*
     * In a try-with-resources block, create context.
     * Create consumer.
     * Receive all text messages from destination until
     * a non-text message is received indicating end of
     * message stream.
     */
    try (JMSContext context = connectionFactory.createContext(); ) {
      consumer = context.createConsumer(dest);
      int count = 0;

      while (true) {
        Message m = consumer.receive(1000);

        if (m != null) {
          if (m instanceof TextMessage) {
            // Comment out the following two lines to receive
            // a large volume of messages
            System.out.println("Reading message: " + m.getBody(String.class));
            count += 1;
          } else {
            break;
          }
        }
      }
      System.out.println("Messages received: " + count);
    } catch (JMSException e) {
      System.err.println("Exception occurred: " + e.toString());
      System.exit(1);
    }
    System.exit(0);
  }