I'm reading about JNDI and DataSource.
- https://www.journaldev.com/2509/java-datasource-jdbc-datasource-example
- https://www.journaldev.com/2513/tomcat-datasource-jndi-example-java
- https://www.baeldung.com/jndi
Does it make sense to use JNDI without a server, e.g. Tomcat, JBoss, WAS?
Is there some simple, standalone application that uses JNDI?
I would like an example using both without a server.
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
public class ClassTest {
public static void main(String[] args) throws RemoteException {
String title = "java:comp/UserTransaction";
String host = "localhost";
int port = 1099;
System.setSecurityManager(new SecurityManager());
//System.setSecurityManager(new RMISecurityManager());
Registry registry = LocateRegistry.getRegistry(host, port);
Hashtable<String, String> env = new Hashtable() {
{
put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");
//put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
put(Context.PROVIDER_URL, "rmi://" + host + ":" + port);
}
};
try {
Context ctx = new InitialContext(env);
ctx.bind(title, new ImplRemote("Apple"));
} catch (Exception ex) {
ex.printStackTrace();
}
try {
Context ctx = new InitialContext(env);
String str = ((ImplRemote) ctx.lookup(title)).getValue();
System.out.println("str:" + str);
} catch (Exception ex) {
ex.printStackTrace();
}
}
static class ImplRemote implements Remote {
private String value;
public ImplRemote(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
}
Executing
$ java -Djava.security.policy=<(echo 'grant {permission java.security.AllPermission;};') ClassTest
javax.naming.ServiceUnavailableException [Root exception is java.rmi.ConnectException: Connection refused to host: localhost; nested exception is:
java.net.ConnectException: Connection refused (Connection refused)]
at com.sun.jndi.rmi.registry.RegistryContext.bind(RegistryContext.java:161)
at com.sun.jndi.rmi.registry.RegistryContext.bind(RegistryContext.java:166)
at javax.naming.InitialContext.bind(InitialContext.java:425)
at ClassTest.main(ClassTest.java:71)
Caused by: java.rmi.ConnectException: Connection refused to host: localhost; nested exception is:
java.net.ConnectException: Connection refused (Connection refused)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:338)
at sun.rmi.registry.RegistryImpl_Stub.bind(RegistryImpl_Stub.java:60)
at com.sun.jndi.rmi.registry.RegistryContext.bind(RegistryContext.java:155)
... 3 more
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:211)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:148)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
... 8 more
javax.naming.ServiceUnavailableException [Root exception is java.rmi.ConnectException: Connection refused to host: localhost; nested exception is:
java.net.ConnectException: Connection refused (Connection refused)]
at com.sun.jndi.rmi.registry.RegistryContext.lookup(RegistryContext.java:136)
at com.sun.jndi.rmi.registry.RegistryContext.lookup(RegistryContext.java:142)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at ClassTest.main(ClassTest.java:78)
Caused by: java.rmi.ConnectException: Connection refused to host: localhost; nested exception is:
java.net.ConnectException: Connection refused (Connection refused)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:338)
at sun.rmi.registry.RegistryImpl_Stub.lookup(RegistryImpl_Stub.java:112)
at com.sun.jndi.rmi.registry.RegistryContext.lookup(RegistryContext.java:132)
... 3 more
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:211)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:148)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
... 8 more
$
Thanks.