I have a problem using java rmi:
When I'm trying to run my server, I get a connectException (see below).
Exception happens when executing the rebind method:
Runtime.getRuntime().exec("rmiregistry 2020");
MyServer server = new MyServer();
Naming.rebind("//localhost:2020/RemoteDataPointHandler", server);
when using rmi://localhost:2020/RemoteDataPointHandler instead, it doesn't work either. Also using the default port does not work. I also tried using the 127.0.0.1 ip-address, but with the same effect.
my runtime args:
-Djava.security.policy=java.security.AllPermission
Exception in thread "main" java.rmi.ConnectException: Connection refused to host: localhost; nested exception is:
java.net.ConnectException: Connection refused
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:574)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:185)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:171)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:306)
at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
at java.rmi.Naming.rebind(Naming.java:160)
at be.fortega.knx.server.Main.(Main.java:25)
at be.fortega.knx.server.Main.main(Main.java:16)
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:433)
at java.net.Socket.connect(Socket.java:524)
at java.net.Socket.connect(Socket.java:474)
at java.net.Socket.(Socket.java:371)
at java.net.Socket.(Socket.java:184)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:22)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:128)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:569)
... 7 more
had a simliar problem with that connection exception. it is thrown either when the registry is not started yet (like in your case) or when the registry is already unexported (like in my case).
but a short comment to the difference between the 2 ways to start the registry:
Runtime.getRuntime().exec("rmiregistry 2020");
runs the rmiregistry.exe in javas bin-directory in a new process and continues parallel with your java code.
LocateRegistry.createRegistry(2020);
the rmi method call starts the registry, returns the reference to that registry remote object and then continues with the next statement.
in your case the registry is not started in time when you try to bind your object
It seems to work when I replace the
Runtime.getRuntime().exec("rmiregistry 2020");
by
LocateRegistry.createRegistry(2020);
anyone an idea why? What's the difference?
You need to have a rmiregistry running before attempting to connect (register) a RMI service with it.
The LocateRegistry.createRegistry(2020) method call creates and exports a registry on the specified port number.
See the documentation for LocateRegistry
One difference we can note in Windows is:
If you use Runtime.getRuntime().exec("rmiregistry 1024");
you can see rmiregistry.exe process will run in your Task Manager
whereas if you use Registry registry = LocateRegistry.createRegistry(1024);
you can not see the process running in Task Manager,
I think Java handles it in a different way.
and this is my server.policy file
Before running the the application, make sure that you killed all your existing
javaw.exe and rmiregistry.exe corresponds to your rmi programs which are
already running.
The following code works for me by using Registry.LocateRegistry() or
Runtime.getRuntime.exec("");
// Standard extensions get all permissions by default
grant {
permission java.security.AllPermission;
};
VM argument
-Djava.rmi.server.codebase=file:\C:\Users\Durai\workspace\RMI2\src\
Code:
package server;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.Remote;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class HelloServer
{
public static void main (String[] argv)
{
try {
if(System.getSecurityManager()==null){
System.setProperty("java.security.policy","C:\\Users\\Durai\\workspace\\RMI\\src\\server\\server.policy");
System.setSecurityManager(new RMISecurityManager());
}
Runtime.getRuntime().exec("rmiregistry 1024");
// Registry registry = LocateRegistry.createRegistry(1024);
// registry.rebind ("Hello", new Hello ("Hello,From Roseindia.net pvt ltd!"));
//Process process = Runtime.getRuntime().exec("C:\\Users\\Durai\\workspace\\RMI\\src\\server\\rmi_registry_start.bat");
Naming.rebind ("//localhost:1024/Hello",new Hello ("Hello,From Roseindia.net pvt ltd!"));
System.out.println ("Server is connected and ready for operation.");
}
catch (Exception e) {
System.out.println ("Server not connected: " + e);
e.printStackTrace();
}
}
}
it seems that you should set your command as an String[],for example:
String[] command = new String[]{"rmiregistry","2020"};
Runtime.getRuntime().exec(command);
it just like the style of main(String[] args).
Related
I've installed GridDB on Ubuntu. I use 2 computers: first computer is used as GridDB server, second as java Client and when i try to connect to GriDB from second computer it throws Exception, but when I run java code in server side it works completely fine. What's the problem? I use this tutorial.
Here is simple java code:
import java.util.Arrays;
import java.util.Properties;
import com.toshiba.mwcloud.gs.Collection;
import com.toshiba.mwcloud.gs.GSException;
import com.toshiba.mwcloud.gs.GridStore;
import com.toshiba.mwcloud.gs.GridStoreFactory;
import com.toshiba.mwcloud.gs.Query;
import com.toshiba.mwcloud.gs.RowKey;
import com.toshiba.mwcloud.gs.RowSet;
// Operaton on Collection data
public class Sample1 {
static class Person {
#RowKey String name;
boolean status;
long count;
byte[] lob;
}
public static void main(String[] args) throws GSException {
// Get a GridStore instance
Properties props = new Properties();
props.setProperty("notificationAddress", "239.0.0.1");
props.setProperty("notificationPort", "31999");
props.setProperty("clusterName", "defaultCluster");
props.setProperty("user", "admin");
props.setProperty("password", "admin");
GridStore store = GridStoreFactory.getInstance().getGridStore(props);
// Create a Collection (Delete if schema setting is NULL)
Collection<String, Person> col = store.putCollection("col01", Person.class);
}
}
here is Exception, when i try to connect from second computer:
com.toshiba.mwcloud.gs.common.GSConnectionException: [145028:JC_BAD_CONNECTION] Failed to connect (address=/127.0.1.1:10001, reason=Connection refused: connect)
at com.toshiba.mwcloud.gs.subnet.NodeConnection.<init>(NodeConnection.java:142)
at com.toshiba.mwcloud.gs.subnet.NodeConnectionPool.resolve(NodeConnectionPool.java:163)
at com.toshiba.mwcloud.gs.subnet.NodeResolver.updateConnectionAndClusterInfo(NodeResolver.java:644)
at com.toshiba.mwcloud.gs.subnet.NodeResolver.prepareConnectionAndClusterInfo(NodeResolver.java:529)
at com.toshiba.mwcloud.gs.subnet.NodeResolver.getPartitionCount(NodeResolver.java:205)
at com.toshiba.mwcloud.gs.subnet.GridStoreChannel$5.execute(GridStoreChannel.java:2106)
at com.toshiba.mwcloud.gs.subnet.GridStoreChannel.executeStatement(GridStoreChannel.java:1675)
at com.toshiba.mwcloud.gs.subnet.GridStoreChannel.executeResolver(GridStoreChannel.java:1912)
at com.toshiba.mwcloud.gs.subnet.GridStoreChannel.resolvePartitionId(GridStoreChannel.java:2103)
at com.toshiba.mwcloud.gs.subnet.SubnetGridStore.putContainer(SubnetGridStore.java:968)
at com.toshiba.mwcloud.gs.subnet.SubnetGridStore.putCollection(SubnetGridStore.java:1024)
at com.toshiba.mwcloud.gs.subnet.SubnetGridStore.putCollection(SubnetGridStore.java:787)
at com.toshiba.mwcloud.gs.subnet.SubnetGridStore.putCollection(SubnetGridStore.java:98)
at pac.Main.main(Main.java:39)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.toshiba.mwcloud.gs.subnet.NodeConnection.<init>(NodeConnection.java:129)
... 13 more
Caused by: java.net.ConnectException: Connection refused: connect
The problem is that the server is not accepting connections from the second machine. This could be due to a number of things. The most likely are:
Your server is not listening for requests on its external IP address. (For example, the DB may be listening on 127.0.0.1 only.) On the server, check what services are listening on the server's external IP address; e.g. https://www.tecmint.com/find-listening-ports-linux/.
Your client may be configured to talk to the wrong server, or to use the wrong port.
Firewalls. (Though the normal firewall behavior would be to drop the connection packets, leading to a different exception.)
There are other possibilities, but the above should be enough to get you started.
If the above haven't identified the problem, you will need to resort to things like:
Check to see what happens when you connect to that database server / port using a TCP diagnostic tool. Does it connect at the TCP level?
Use a network packet sniffer to see what happens to the TCP packets when the client tries to connect to the database.
Check your route tables and IP tables for strange routing rules. If you are using a virtual machine, check at the hypervisor level too.
239.0.0.1 is a multicast address. And often it needs to do some additional steps with OS, router settings to enable multicast.
So it makes sense to check if multicast is enabled: https://serverfault.com/questions/294207/how-can-i-test-multicast-udp-connectivity-between-two-servers
And also you could check that the IP/port, with which the GridDB node is registered in the cluster, is accessible.
The IP address can be obtained with next command:
$ gs_stat -u admin/admin
We are using TCP server.sometimes we get following exception
java.net.SocketTimeoutException: Accept timed out
at java.net.PlainSocketImpl.socketAccept(Native Method)
at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:398)
at java.net.ServerSocket.implAccept(ServerSocket.java:530)
at java.net.ServerSocket.accept(ServerSocket.java:498)
I googled it and found out that it happened when exceed timout which is setby setSoTimeout(timeinmilli)method. but, we didnt invoke that methode.
sample tcp server:
class TCPServer
{
public static void main(String args[]){
public void initializeConnectionHandler(String ip, int port) {
try{
ServerSocket serverSocket = new ServerSocket(port, serverSocket_backlog, InetAddress.getByName(ip));
log("Waiting for client on port: " +serverSocket.getLocalPort());
while(true){
Socket socket = serverSocket.accept();
log("Just connected to "+ socket.getRemoteSocketAddress());
Runnable tcpConnectionHandler = new TCPConnectionHandler(serverSocket, socket, workerManager);
new Thread(tcpConnectionHandler).start();
}
}
catch (Exception e){
log("Exception occured while initializing ConnectionHandlers: "+e);
logException(e);
System.exit(0);
}
}
}
}
You're right that this exception should only occur if you've set a timeout by calling setSoTimeout. Nevertheless if you go through the search results you can find occasional discussions about this effect as you've seen it, e.g. in Tomcat where they assumed that they found a bug in the JVM and added a workaround to keep the server running.
Another result (german language, sorry) reporting about this exception with Jenkins indicates that there might be problems when IPv6 is active as well. Solution there was to set the system property java.net.preferIPv4Stack to true.
I'm not a fan of the latter "solution" but concerning the workaround the Tomcat-team implemented, I suggest you do the same. At least, don't stop the whole loop in case of an exception like this.
Internally it is calling socketAccept method of TwoStacksPlainSocketImpl class.
java.net.SocketTimeoutException: Accept timed out
at java.net.PlainSocketImpl.socketAccept(Native Method)
at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:398)
at java.net.ServerSocket.implAccept(ServerSocket.java:530)
at java.net.ServerSocket.accept(ServerSocket.java:498)
Method is native so you can't completely find the reason but below is the native code from openjdk.
https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/windows/native/java/net/TwoStacksPlainSocketImpl.c
Where you can find it is throwing mentioned exception.
In this post java H2 hanging on getLocalhost on arm32 device I had a problem creating database using Hibernate. I have now managed to remove track down the issue to core java with this test program, InetAddress lookup using getByName("localhost") works, but getLocalHost() fails !
Points towards configuration issue with linux machine. localhost is in /etc/hosts
But I am no Linux expert, what could cause this ?
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Addresses
{
public static void main(String[] args) {
try {
InetAddress address= InetAddress.getByName("localhost");
System.out.println(address.getHostName()+ "-"+address.getHostAddress());
address= InetAddress.getLocalHost();
System.out.println(address.getHostName()+ "-"+address.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
Gives
localhost-127.0.0.1
java.net.UnknownHostException: N1-ZS10: N1-ZS10: Name or service not known
at java.net.InetAddress.getLocalHost(InetAddress.java:1505)
at Addresses.main(Addresses.java:12)
Caused by: java.net.UnknownHostException: N1-ZS10: Name or service not known
at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928)
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323)
at java.net.InetAddress.getLocalHost(InetAddress.java:1500)
... 1 more
I am very new to JMX. I am trying to log the tomcat statistics like threads used, cache, sessions and other standard values. I am trying to achieve this with java code.
I have done the following things as of now. (I am trying to access the values of a local tomcat 6.0 monitor on windows)
1)I have added the following options in the catalina.bat
set CATALINA_OPTS=-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9004 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false
After that I restarted the tomcat server.
2) Then I wrote the following code.
package com.ss.fg;
import java.lang.management.ManagementFactory;
import javax.management.MBeanServer;
import javax.management.ObjectName;
public class SystemConfigManagement
{
static MBeanServer connection = ManagementFactory.getPlatformMBeanServer();
public static void main(String[] args) throws Exception {
getActiveSession();
}
public static void getActiveSession()throws Exception
{
ObjectName name=new ObjectName("Catalina:type=Manager,path=/MMDisplay,host=localhost");
String attrValue = ManagementFactory.getPlatformMBeanServer().getAttribute(name, "activeSessions").toString();
System.out.println(attrValue);
}
}
I even tried context instead of path.
I am getting the following exception
Exception in thread "main" javax.management.InstanceNotFoundException: Catalina:type=Manager,path=/MMDisplay,host=localhost
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(Unknown Source)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getAttribute(Unknown Source)
at com.sun.jmx.mbeanserver.JmxMBeanServer.getAttribute(Unknown Source)
at com.softsmith.floodgates.SystemConfigManagement.getActiveSession(SystemConfigManagement.java:15)
at com.softsmith.floodgates.SystemConfigManagement.main(SystemConfigManagement.java:10)
How can I resolve this issue?
Should I add Some jar files, or should I do some other settings..
Please help
When using
MBeanServer connection = ManagementFactory.getPlatformMBeanServer();
you are actually connectiong to the MBean-Server of the JVM running your program, not to that of your tomcat-instance, so it doesn't know of the Catalina-MBeans.
To establish a connection to a remote jvm try something like:
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://servername:9999/jmxrmi");
JMXConnector connector = JMXConnectorFactory.connect(url);
connector.connect();
MBeanServerConnection server = connector.getMBeanServerConnection();
//do work here
ObjectName name = new ObjectName("Catalina:type=Manager,path=/manager,host=localhost");
String attrValue = mb.getAttribute(name, "activeSessions").toString();
System.out.println(attrValue);
//..and don't forget to close the connection
connector.close();
If the error is still there make sure you are using the correct object-name.
When i using
MBeanServer connection = ManagementFactory.getPlatformMBeanServer();
i am actually connectiong to the MBean-Server of the JVM running my program, not to that of my tomcat-instance, so it doesn't know of the Catalina-MBeans.
so i try to connected remotly to jmx but have error like this :
java.io.IOException: Failed to retrieve RMIServer stub: javax.naming.CommunicationException [Root exception is java.rmi.ConnectIOException: error during JRMP connection establishment; nested exception is....
and now i solve :
for first i added catalina-jmx-remote.jar to TOMCAT_HOME/lib directory of Tomcat and then configure the listener on server.xml i added the following
snippet under the tag :
<Listener className="org.apache.catalina.mbeans.JmxRemoteLifecycleListener" rmiRegistryPortPlatform="10001" rmiServerPortPlatform="10002" useLocalPorts="true" />
and finally i set the following sentence in my catalina.bat :
JAVA_OPTS="$JAVA_OPTS -Djava.rmi.server.hostname=localhost -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false"
I have a server program running on my laptop, same router and same code. It work's fine and clients can connect. However when I copied the workspace to my PC and when I run it, I get this nonsense:
IO error java.net.SocketException: select failed
Here is the code...
public static void main(String[] args) {
System.out.println("running server!");
int nreq = 1;
try{
ServerSocket sock = new ServerSocket(7331);
for(;;){
Socket newsock = sock.accept();
System.out.println("Creating thread...");
//Thread t = new ThreadHandler(newsock, nreq);
//t.start();
nreq++;
}
}
catch(Exception e)
{
System.out.println("IO error " + e);
}
Basically what could cause this error (of which I can find no useful information online) to happen on one PC and not another? It has to be something about the PC itself. I am most confused. Basically it can't open a socket?
Here is the full stacktrace:
java.net.SocketException: select failed
at java.net.PlainSocketImpl.socketAccept(Native Method)
at java.net.PlainSocketImpl.accept(Unknown Source)
at java.net.ServerSocket.implAccept(Unknown Source)
at java.net.ServerSocket.accept(Unknown Source)
at Server.main(Server.java:18)
If it's relevant, I'm using Windows 7 64 bit Ultimate on the PC which is giving me problems. The laptop that it works fine on is Windows 7 32 bit. So the only discernible difference I can tell is 32 vs 64 bit.
Could this be relevant? I don't understand it.
http://forums.codeguru.com/showthread.php?522257-Windows-Sockets-64-bit
I've run into this because of permissions. I'm more accustomed to Linux where I would need to check the settings for IPTables (or disable it), permissions such as non-root users binding to ports < 1024. I believe on Windows you'll want to check your user's administrative rights and your Windows Firewall.