public void onMetricsServiceReady(@Observes @ServiceReady ServiceReadyEvent event) {
   context = connectionFactory.createContext();
   gaugesConsumer = context.createConsumer(gaugesQueue);
   gaugesConsumer.setMessageListener(
       new BasicMessageListener<MetricDataMessage>() {
         @Override
         protected void onBasicMessage(MetricDataMessage basicMessage) {
           onGaugeData(basicMessage.getMetricData());
         }
       });
   countersConsumer = context.createConsumer(countersQueue);
   countersConsumer.setMessageListener(
       new BasicMessageListener<MetricDataMessage>() {
         @Override
         protected void onBasicMessage(MetricDataMessage basicMessage) {
           onCounterData(basicMessage.getMetricData());
         }
       });
   availabilityConsumer = context.createConsumer(availabilityQueue);
   availabilityConsumer.setMessageListener(
       new BasicMessageListener<AvailDataMessage>() {
         @Override
         protected void onBasicMessage(AvailDataMessage basicMessage) {
           onAvailData(basicMessage.getAvailData());
         }
       });
 }
Пример #2
0
  @Test
  public void sendToNonExistantDestination() throws Exception {
    Destination destination = HornetQJMSClient.createQueue("DoesNotExist");
    TransportConfiguration transportConfiguration =
        new TransportConfiguration(InVMConnectorFactory.class.getName());
    ConnectionFactory localConnectionFactory =
        HornetQJMSClient.createConnectionFactoryWithoutHA(
            JMSFactoryType.CF, transportConfiguration);
    // Using JMS 1 API
    Connection connection = localConnectionFactory.createConnection();
    Session session = connection.createSession();
    try {
      MessageProducer messageProducer = session.createProducer(null);
      messageProducer.send(destination, session.createMessage());
      Assert.fail("Succeeded in sending message to a non-existant destination using JMS 1 API!");
    } catch (JMSException e) { // Expected }

    }

    // Using JMS 2 API
    JMSContext context = localConnectionFactory.createContext();
    JMSProducer jmsProducer = context.createProducer().setDeliveryMode(DeliveryMode.PERSISTENT);

    try {
      jmsProducer.send(destination, context.createMessage());
      Assert.fail("Succeeded in sending message to a non-existant destination using JMS 2 API!");
    } catch (JMSRuntimeException e) { // Expected }
    }
  }
  protected static void sendMessage(Context ctx, String destinationLookup, String text)
      throws NamingException {
    ConnectionFactory cf = (ConnectionFactory) ctx.lookup("jms/RemoteConnectionFactory");
    assertNotNull(cf);
    Destination destination = (Destination) ctx.lookup(destinationLookup);
    assertNotNull(destination);

    try (JMSContext context = cf.createContext("guest", "guest")) {
      context.createProducer().send(destination, text);
    }
  }
  protected static void receiveMessage(Context ctx, String destinationLookup, String expectedText)
      throws NamingException {
    ConnectionFactory cf = (ConnectionFactory) ctx.lookup("jms/RemoteConnectionFactory");
    assertNotNull(cf);
    Destination destination = (Destination) ctx.lookup(destinationLookup);
    assertNotNull(destination);

    try (JMSContext context = cf.createContext("guest", "guest"); ) {
      JMSConsumer consumer = context.createConsumer(destination);
      receiveMessage(consumer, expectedText);
    }
  }
Пример #5
0
  public static void main(String[] args) {
    connectionFactory = lookup(ConnectionFactory.class, JNDI_CONNECTION_FACTORY);
    topic = lookup(Topic.class, JNDI_TOPIC);

    context = connectionFactory.createContext();

    JFrame frame = new JFrame("KwetterMonitorFrame");
    frame.setContentPane(new KwetterMonitorFrame().formPanel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setSize(900, 500);
    frame.setVisible(true);
  }
 public static void main(String[] args) throws NamingException {
   final InitialContext ic = new InitialContext();
   ConnectionFactory factory = (ConnectionFactory) ic.lookup("jms/RemoteConnectionFactory");
   Queue queue = (Queue) ic.lookup("jms/FILA.GERADOR");
   try (JMSContext context = factory.createContext("jms", "jms2")) {
     JMSConsumer consumer = context.createConsumer(queue);
     consumer.setMessageListener(new TratadorDeMensagem());
     context.start();
     Scanner teclado = new Scanner(System.in);
     System.out.println("Tratador esperando as mensagens na fila JMS");
     teclado.nextLine();
     teclado.close();
     context.stop();
   }
 }
Пример #7
0
  public static void main(String[] args) {
    MapMessage outMessage;
    JMSConsumer orderConfirmReceiver;
    MapMessage inMessage;

    if (args.length != 1) {
      System.out.println("Error: Program takes numerical argument.");
      System.exit(1);
    }

    quantity = (new Integer(args[0])).intValue();
    System.out.println("Retailer: Quantity to be ordered is " + quantity);
    /*
     * Create non-transacted context and sender for
     * vendor order queue.
     * Create message to vendor, setting item and
     * quantity values.
     * Send message.
     * Create receiver for retailer confirmation queue.
     * Get message and report result.
     * Send an end-of-message-stream message so vendor
     * will stop processing orders.
     */
    try (JMSContext context = connectionFactory.createContext(); ) {
      outMessage = context.createMapMessage();
      outMessage.setString("Item", "Computer(s)");
      outMessage.setInt("Quantity", quantity);
      outMessage.setJMSReplyTo(retailerConfirmQueue);
      context.createProducer().send(vendorOrderQueue, outMessage);
      System.out.println("Retailer: Ordered " + quantity + " computer(s)");

      orderConfirmReceiver = context.createConsumer(retailerConfirmQueue);
      inMessage = (MapMessage) orderConfirmReceiver.receive();

      if (inMessage.getBoolean("OrderAccepted") == true) {
        System.out.println("Retailer: Order filled");
      } else {
        System.out.println("Retailer: Order not filled");
      }

      System.out.println("Retailer: Placing another order");
      outMessage.setInt("Quantity", quantity * 2);
      context.createProducer().send(vendorOrderQueue, outMessage);
      System.out.println("Retailer: Ordered " + outMessage.getInt("Quantity") + " computer(s)");
      inMessage = (MapMessage) orderConfirmReceiver.receive();

      if (inMessage.getBoolean("OrderAccepted") == true) {
        System.out.println("Retailer: Order filled");
      } else {
        System.out.println("Retailer: Order not filled");
      }

      /*
       * Send a non-text control message indicating end
       * of messages.
       */
      context.createProducer().send(vendorOrderQueue, context.createMessage());
    } catch (JMSException e) {
      System.err.println("Retailer: Exception occurred: " + e.toString());
    }
  }
 protected static JMSContext createJMSContext(Context ctx) throws NamingException {
   ConnectionFactory cf = (ConnectionFactory) ctx.lookup("jms/RemoteConnectionFactory");
   assertNotNull(cf);
   JMSContext context = cf.createContext("guest", "guest");
   return context;
 }
Пример #9
0
 protected final JMSContext createContext(int sessionMode) {
   return addContext(cf.createContext(null, null, sessionMode));
 }
Пример #10
0
 protected final JMSContext createContext() {
   return addContext(cf.createContext());
 }
Пример #11
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);
  }