B
bumsys
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?
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?