Example usage for javax.naming InitialContext InitialContext

List of usage examples for javax.naming InitialContext InitialContext

Introduction

In this page you can find the example usage for javax.naming InitialContext InitialContext.

Prototype

public InitialContext() throws NamingException 

Source Link

Document

Constructs an initial context.

Usage

From source file:com.fusesource.examples.activemq.SimplePublisher.java

public static void main(String args[]) {
    Connection connection = null;

    try {//  w ww.  j  a  v  a 2 s  . c om
        // JNDI lookup of JMS Connection Factory and JMS Destination
        Context context = new InitialContext();
        ConnectionFactory factory = (ConnectionFactory) context.lookup(CONNECTION_FACTORY_NAME);

        connection = factory.createConnection();
        connection.start();

        Session session = connection.createSession(NON_TRANSACTED, Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic(DESTINATION_NAME);

        MessageProducer producer = session.createProducer(topic);

        producer.setTimeToLive(MESSAGE_TIME_TO_LIVE_MILLISECONDS);

        for (int i = 1; i <= NUM_MESSAGES_TO_BE_SENT; i++) {
            TextMessage message = session.createTextMessage(i + ". message sent");
            LOG.info("Sending to destination: " + DESTINATION_NAME + " this text: '" + message.getText());
            producer.send(message);
            Thread.sleep(MESSAGE_DELAY_MILLISECONDS);
        }

        // Cleanup
        producer.close();
        session.close();
    } catch (Throwable t) {
        LOG.error(t);
    } finally {
        // Cleanup code
        // In general, you should always close producers, consumers,
        // sessions, and connections in reverse order of creation.
        // For this simple example, a JMS connection.close will
        // clean up all other resources.
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                LOG.error(e);
            }
        }
    }
}

From source file:EmployeeBean.java

  public static void main(String[] a) throws Exception {

  EmployeeServiceRemote service = null;//from  w  w  w  .j a v a  2s. c  o m

  // Context compEnv = (Context) new InitialContext().lookup("java:comp/env");

  // service = (HelloService)new InitialContext().lookup("java:comp/env/ejb/HelloService");
  service = (EmployeeServiceRemote) new InitialContext().lookup("MyEmployeeBean/remote");

  service.doAction();

}

From source file:Employee.java

public static void main(String[] a) throws Exception {

        EmployeeServiceRemote service = null;

        // Context compEnv = (Context) new InitialContext().lookup("java:comp/env");

        // service = (HelloService)new InitialContext().lookup("java:comp/env/ejb/HelloService");
        service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeService/remote");

        service.doAction();// w w  w . j av a  2s . c o m

    }

From source file:Employee.java

public static void main(String[] a) throws Exception {

        EmployeeServiceRemote service = null;

        // Context compEnv = (Context) new InitialContext().lookup("java:comp/env");

        // service = (HelloService)new InitialContext().lookup("java:comp/env/ejb/HelloService");
        service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeBean/remote");

        service.doAction();//  www  .  ja v  a 2s.co m

    }

From source file:Employee.java

  public static void main(String[] a) throws Exception {

  EmployeeServiceRemote service = null;//from   w ww .j a v a 2  s.co m

  // Context compEnv = (Context) new InitialContext().lookup("java:comp/env");

  // service = (HelloService)new InitialContext().lookup("java:comp/env/ejb/HelloService");
  service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeService/remote");
    
    


    
  service.doAction();

}

From source file:io.datalayer.activemq.consumer.SimpleQueueReceiver.java

/**
 * Main method.//from   ww  w . j a v  a 2 s  .  co m
 * 
 * @param args the queue used by the example
 */
public static void main(String... args) {
    String queueName = null;
    Context jndiContext = null;
    QueueConnectionFactory queueConnectionFactory = null;
    QueueConnection queueConnection = null;
    QueueSession queueSession = null;
    Queue queue = null;
    QueueReceiver queueReceiver = null;
    TextMessage message = null;

    /*
     * Read queue name from command line and display it.
     */
    if (args.length != 1) {
        LOG.info("Usage: java " + "SimpleQueueReceiver <queue-name>");
        System.exit(1);
    }
    queueName = args[0];
    LOG.info("Queue name is " + queueName);

    /*
     * Create a JNDI API InitialContext object if none exists yet.
     */
    try {
        jndiContext = new InitialContext();
    } catch (NamingException e) {
        LOG.info("Could not create JNDI API " + "context: " + e.toString());
        System.exit(1);
    }

    /*
     * Look up connection factory and queue. If either does not exist, exit.
     */
    try {
        queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("QueueConnectionFactory");
        queue = (Queue) jndiContext.lookup(queueName);
    } catch (NamingException e) {
        LOG.info("JNDI API lookup failed: " + e.toString());
        System.exit(1);
    }

    /*
     * Create connection. Create session from connection; false means
     * session is not transacted. Create receiver, then start message
     * delivery. Receive all text messages from queue until a non-text
     * message is received indicating end of message stream. Close
     * connection.
     */
    try {
        queueConnection = queueConnectionFactory.createQueueConnection();
        queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        queueReceiver = queueSession.createReceiver(queue);
        queueConnection.start();
        while (true) {
            Message m = queueReceiver.receive(1);
            if (m != null) {
                if (m instanceof TextMessage) {
                    message = (TextMessage) m;
                    LOG.info("Reading message: " + message.getText());
                } else {
                    break;
                }
            }
        }
    } catch (JMSException e) {
        LOG.info("Exception occurred: " + e.toString());
    } finally {
        if (queueConnection != null) {
            try {
                queueConnection.close();
            } catch (JMSException e) {
            }
        }
    }
}

From source file:io.datalayer.activemq.consumer.SimpleConsumer.java

/**
 * @param args the queue used by the example
 *//*ww  w  .j  a  va2s . c  o  m*/
public static void main(String... args) {
    String destinationName = null;
    Context jndiContext = null;
    ConnectionFactory connectionFactory = null;
    Connection connection = null;
    Session session = null;
    Destination destination = null;
    MessageConsumer consumer = null;

    /*
     * Read destination name from command line and display it.
     */
    if (args.length != 1) {
        LOG.info("Usage: java SimpleConsumer <destination-name>");
        System.exit(1);
    }
    destinationName = args[0];
    LOG.info("Destination name is " + destinationName);

    /*
     * Create a JNDI API InitialContext object
     */
    try {
        jndiContext = new InitialContext();
    } catch (NamingException e) {
        LOG.info("Could not create JNDI API " + "context: " + e.toString());
        System.exit(1);
    }

    /*
     * Look up connection factory and destination.
     */
    try {
        connectionFactory = (ConnectionFactory) jndiContext.lookup("ConnectionFactory");
        destination = (Destination) jndiContext.lookup(destinationName);
    } catch (NamingException e) {
        LOG.info("JNDI API lookup failed: " + e.toString());
        System.exit(1);
    }

    /*
     * Create connection. Create session from connection; false means
     * session is not transacted. Create receiver, then start message
     * delivery. Receive all text messages from destination until a non-text
     * message is received indicating end of message stream. Close
     * connection.
     */
    try {
        connection = connectionFactory.createConnection();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        consumer = session.createConsumer(destination);
        connection.start();
        while (true) {
            Message m = consumer.receive(1);
            if (m != null) {
                if (m instanceof TextMessage) {
                    TextMessage message = (TextMessage) m;
                    LOG.info("Reading message: " + message.getText());
                } else {
                    break;
                }
            }
        }
    } catch (JMSException e) {
        LOG.info("Exception occurred: " + e);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
            }
        }
    }
}

From source file:io.datalayer.activemq.producer.SimpleQueueSender.java

/**
 * Main method.//from   w  ww  . j a  v a 2s.c  o  m
 * 
 * @param args the queue used by the example and, optionally, the number of
 *                messages to send
 */
public static void main(String... args) {
    String queueName = null;
    Context jndiContext = null;
    QueueConnectionFactory queueConnectionFactory = null;
    QueueConnection queueConnection = null;
    QueueSession queueSession = null;
    Queue queue = null;
    QueueSender queueSender = null;
    TextMessage message = null;
    final int numMsgs;

    if ((args.length < 1) || (args.length > 2)) {
        LOG.info("Usage: java SimpleQueueSender " + "<queue-name> [<number-of-messages>]");
        System.exit(1);
    }
    queueName = args[0];
    LOG.info("Queue name is " + queueName);
    if (args.length == 2) {
        numMsgs = (new Integer(args[1])).intValue();
    } else {
        numMsgs = 1;
    }

    /*
     * Create a JNDI API InitialContext object if none exists yet.
     */
    try {
        jndiContext = new InitialContext();
    } catch (NamingException e) {
        LOG.info("Could not create JNDI API context: " + e.toString());
        System.exit(1);
    }

    /*
     * Look up connection factory and queue. If either does not exist, exit.
     */
    try {
        queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup("QueueConnectionFactory");
        queue = (Queue) jndiContext.lookup(queueName);
    } catch (NamingException e) {
        LOG.info("JNDI API lookup failed: " + e);
        System.exit(1);
    }

    /*
     * Create connection. Create session from connection; false means
     * session is not transacted. Create sender and text message. Send
     * messages, varying text slightly. Send end-of-messages message.
     * Finally, close connection.
     */
    try {
        queueConnection = queueConnectionFactory.createQueueConnection();
        queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        queueSender = queueSession.createSender(queue);
        message = queueSession.createTextMessage();
        for (int i = 0; i < numMsgs; i++) {
            message.setText("This is message " + (i + 1));
            LOG.info("Sending message: " + message.getText());
            queueSender.send(message);
        }

        /*
         * Send a non-text control message indicating end of messages.
         */
        queueSender.send(queueSession.createMessage());
    } catch (JMSException e) {
        LOG.info("Exception occurred: " + e.toString());
    } finally {
        if (queueConnection != null) {
            try {
                queueConnection.close();
            } catch (JMSException e) {
            }
        }
    }
}

From source file:io.datalayer.activemq.producer.SimpleProducer.java

/**
 * @param args the destination name to send to and optionally, the number of
 *                messages to send// ww  w . ja va 2  s  .c o m
 */
public static void main(String... args) {
    Context jndiContext = null;
    ConnectionFactory connectionFactory = null;
    Connection connection = null;
    Session session = null;
    Destination destination = null;
    MessageProducer producer = null;
    String destinationName = null;
    final int numMsgs;

    if ((args.length < 1) || (args.length > 2)) {
        LOG.info("Usage: java SimpleProducer <destination-name> [<number-of-messages>]");
        System.exit(1);
    }
    destinationName = args[0];
    LOG.info("Destination name is " + destinationName);
    if (args.length == 2) {
        numMsgs = (new Integer(args[1])).intValue();
    } else {
        numMsgs = 1;
    }

    /*
     * Create a JNDI API InitialContext object
     */
    try {
        jndiContext = new InitialContext();
    } catch (NamingException e) {
        LOG.info("Could not create JNDI API context: " + e.toString());
        System.exit(1);
    }

    /*
     * Look up connection factory and destination.
     */
    try {
        connectionFactory = (ConnectionFactory) jndiContext.lookup("ConnectionFactory");
        destination = (Destination) jndiContext.lookup(destinationName);
    } catch (NamingException e) {
        LOG.info("JNDI API lookup failed: " + e);
        System.exit(1);
    }

    /*
     * Create connection. Create session from connection; false means
     * session is not transacted. Create sender and text message. Send
     * messages, varying text slightly. Send end-of-messages message.
     * Finally, close connection.
     */
    try {
        connection = connectionFactory.createConnection();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        producer = session.createProducer(destination);
        TextMessage message = session.createTextMessage();
        for (int i = 0; i < numMsgs; i++) {
            message.setText("This is message " + (i + 1));
            LOG.info("Sending message: " + message.getText());
            producer.send(message);
        }

        /*
         * Send a non-text control message indicating end of messages.
         */
        producer.send(session.createMessage());
    } catch (JMSException e) {
        LOG.info("Exception occurred: " + e);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
            }
        }
    }
}

From source file:com.itcs.commons.email.impl.NoReplySystemMailSender.java

/**
 * Singleton pattern//from w w w  .j  a v  a  2  s . co m
 * @return
 * @throws NamingException 
 */
private static Session getSession() throws NamingException {
    if (noReplyGodeskEmailSession == null) {
        Context ctx = new InitialContext();
        noReplyGodeskEmailSession = (Session) ctx.lookup("mail/noReplyGodeskEmailSession");
    }
    return noReplyGodeskEmailSession;
}