0

I've just started with Java ee 7, and here is something I couldnt get the idea of how it magically works.

I follow the example from the book Beginning Java EE 7 by Antonio Goncalves. I managed to compile and deploye the code of chapter 13 (about JMS) without any problem. Messages are sent and received as expected, but that make me confused.

The source code is composite of a consumer class, a producer class, a POJO and a MDB class.

here is the consumer:

public class OrderConsumer {

  public static void main(String[] args) throws NamingException {

    // Gets the JNDI context
    Context jndiContext = new InitialContext();

    // Looks up the administered objects
    ConnectionFactory connectionFactory = (ConnectionFactory) jndiContext.lookup("jms/javaee7/ConnectionFactory");
    Destination topic = (Destination) jndiContext.lookup("jms/javaee7/Topic");

    // Loops to receive the messages
    System.out.println("\nInfinite loop. Waiting for a message...");
    try (JMSContext jmsContext = connectionFactory.createContext()) {
      while (true) {
        OrderDTO order = jmsContext.createConsumer(topic).receiveBody(OrderDTO.class);
        System.out.println("Order received: " + order);
      }
    }
  }
}

the producer:

public class OrderProducer {

  public static void main(String[] args) throws NamingException {

    if (args.length != 1) {
      System.out.println("usage : enter an amount");
      System.exit(0);
    }

    System.out.println("Sending message with amount = " + args[0]);

    // Creates an orderDto with a total amount parameter
    Float totalAmount = Float.valueOf(args[0]);
    OrderDTO order = new OrderDTO(1234l, new Date(), "Serge Gainsbourg", totalAmount);

    // Gets the JNDI context
    Context jndiContext = new InitialContext();

    // Looks up the administered objects
    ConnectionFactory connectionFactory = (ConnectionFactory) jndiContext.lookup("jms/javaee7/ConnectionFactory");
    Destination topic = (Destination) jndiContext.lookup("jms/javaee7/Topic");

    try (JMSContext jmsContext = connectionFactory.createContext()) {
      // Sends an object message to the topic
      jmsContext.createProducer().setProperty("orderAmount", totalAmount).send(topic, order);
      System.out.println("\nOrder sent : " + order.toString());
    }
  }
}

the MDB :

@MessageDriven(mappedName = "jms/javaee7/Topic", activationConfig = {
        @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
        @ActivationConfigProperty(propertyName = "messageSelector", propertyValue = "orderAmount > 1000")
})
public class ExpensiveOrderMDB implements MessageListener {

  public void onMessage(Message message) {
    try {
      OrderDTO order = message.getBody(OrderDTO.class);
      System.out.println("Expensive order received: " + order.toString());
    } catch (JMSException e) {
      e.printStackTrace();
    }
  }
}

Content of msg encapsulated in a POJO object which implements Serializable interface

ExpensiveOrderMDB and the POJO is packaged in a .jar file and deploy in glassfish server running locally. Connection and desitination resouces are created by asadmin.

Question is: How can the consumer and producer know that the connection and destination are available on local glassfish server for it to make a connection and send/receive msg? (The lines that create connection and destination say nothing about local glassfish server)

jAckOdE
  • 2,402
  • 8
  • 37
  • 67

1 Answers1

0

Probably there is a jndi.properties file in which the connection to the glassfish is defined

andi
  • 240
  • 1
  • 2
  • 10