JMS + glassfish

Discussion in 'Java' started by bumsys@gmail.com, Feb 3, 2009.

  1. Guest

    I write an application for using java message service. Here is a code:

    import java.io.*;
    import java.util.Properties;
    import java.util.Hashtable;

    import javax.jms.*;
    import javax.naming.*;

    public class Sender {

    public static void main(String[] args) {

    new Sender().send();
    }

    public void send() {

    BufferedReader reader = new BufferedReader(new
    InputStreamReader(System.in));

    try {
    //Zapros JNDI-name
    System.out.println("Enter ConnectionFactory name:");
    String factoryName = "jms/ConnectionFactory";
    System.out.println("Enter Destination name:");
    String destinationName = "jms/Queue";

    //Search administration objects
    InitialContext initContext = new InitialContext();
    ConnectionFactory factory =
    (ConnectionFactory) initContext.lookup(factoryName);

    Destination destination = (Destination) initContext.lookup
    (destinationName);
    initContext.close();

    //Create JMS-objects
    Connection connection = factory.createConnection();
    Session session =
    connection.createSession(false,
    Session.AUTO_ACKNOWLEDGE);
    MessageProducer sender = session.createProducer
    (destination);

    //Send messages
    String messageText = null;
    while (true) {
    System.out.println("Enter message to send or
    'quit':");
    messageText = reader.readLine();
    if ("quit".equals(messageText))
    break;
    TextMessage message = session.createTextMessage
    (messageText);
    sender.send(message);
    }

    //Exit
    System.out.println("Exiting...");
    reader.close();
    connection.close();
    System.out.println("Goodbye!");

    } catch (Exception e) {
    e.printStackTrace();
    System.exit(1);
    }
    }
    }

    And I have error:

    javax.naming.NoInitialContextException: Need to specify class name in
    environment or system property, or as an applet parameter, or in an
    application resource file: java.naming.factory.initial
    at javax.naming.spi.NamingManager.getInitialContext
    (NamingManager.java:645)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:
    288)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx
    (InitialContext.java:325)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at jms.Sender.send(Sender.java:53)
    at jms.Sender.main(Sender.java:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke
    (NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke
    (DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:
    90)

    I work in intellij idea 8.0.1 and use server glassfish.

    May be it is necessary to indicate some Properties when create
    InitialContext. If yes, can someone write these properties?
     
    , Feb 3, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Stein
    Replies:
    0
    Views:
    783
    Thomas Stein
    Oct 18, 2004
  2. glassfish?

    , May 24, 2006, in forum: Java
    Replies:
    1
    Views:
    483
  3. Replies:
    0
    Views:
    373
  4. Replies:
    0
    Views:
    441
  5. Replies:
    0
    Views:
    918
Loading...

Share This Page