Java RMI - check if registry exists - java

I'm having a little trouble with Java RMI.
Am I able to check if a registry exists?
This line of code is supposed to give me the registry.
LocateRegistry.getRegistry(ip, Registry.REGISTRY_PORT);
But when I call it with a wrong IP address or an IP address where no registry can be found, the method gets stuck.
So my question is, can i somehow check if there is a registry at a certain IP address BEFORE calling getRegistry()?

No. In any case the best way to test the availability of any resource is to try to use it. In this case, call lookup() and catch the exception. You have to do that anyway, so doing it twice is pretty pointless.

Related

com.jacob.com.ComFailException: Can't map name to dispid. Random failures

We have developed a COM Object which is an .exe file and runs as a service in background. We are trying to connect to the object using JACOB.
Introduction:
On start up create an activeXcomponent and call Connect method of the object. Connect method returns instance of the class (which is written inside COM object) by reference, here referred as "handle"
component=new ActiveXComponent("ServiceName.className");
Dispatch.call(component,FN_CONNECT,sourceType,additionalParams,handle);
This handle is stored in a class level variable. Instance of this JAVA Class is stored in a connection pool and is later used for future function calls. This is to ensure that the next calls are called on objects/pointers which are already initialized.
When data fetch call is to be initiated, it calls another method named fetchData and takes request as input. It returns error code and response by reference. The component object is referred through the instance of a JAVA class that was stored in the connection pool
Variant response = new Variant("", true); //String
Variant errorCode = new Variant(0, true);
Dispatch.call(component,FETCH_DATA,handle,functionCode,request,response,errorCode);
Problem statement:
Now while testing in local enviroment we found out that everthing works fine as expected and no exception is thrown. However in client enviroment we are getting following error:
Caused by: com.jacob.com.ComFailException: Can't map name to dispid: fetchData
at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.com.Dispatch.invokev(Dispatch.java:625)
at com.jacob.com.Dispatch.callN(Dispatch.java:453)
at com.jacob.com.Dispatch.call(Dispatch.java:541)
We managed to notice that same code works fine at one attempt and fails in next restart. This behavior is completely random hence difficult to trace.
Points to note:
fetchData function exists with exactly same signature at both side.
Code runs fine at one point and misbehaves after restart.
We read somewhere, JACOB Library misbehaves in a multi-threaded enviroment. Is this true?
If yes, what should be done in order to make it work as expected.
If not, then why is this exception thrown randomly ?
Any idea/help/lead is useful. Thanks in advance!
This is not an answer to this specific situation, since the question is very old anyway - but just since I see it unanswered and I saw it in one of my searches, I'd like to give some pointers.
In my own experience, JACOB is okay when you use it correctly - so it can't be called "misbehaving".
In my specific scenario, again, since I am using JACOB to automate Microsoft Word, most of the problems we have been having stem from the fact that Office is not made to be automated on backend, window-less environments.
Link to a Microsoft article on early and late binding in COM automation
Other related threads on StackOverflow are:
JACOB library fails when used in multiple threads
Java - Jacob Multitheading
JACOB doesn't release the objects properly
In one of the above threads they mention an old archived article about JACOB and how they suggest to use it in multi-threaded environments.

Reuse Provide in Guice based on parameters

I've tried searching for this but haven't really found a solution so decided to post a question.
I'm working on an application where a user will input an IP (an SNMP device) and my application needs to connect to it and work with it. During runtime of the application, the user may provide another IP address and then I need to connect to the second one also keeping both the connections alive (as singletons).
My problem is I'm not able to wrap my head around this conceptually. My connection module is right now something like the following:
#Override
Configure() {
String ip = first ip;
}
#Provides
Connect connect() {
// connect to ip
return connection;
}
Can anyone give me some hints here?
You should probably pass the IP address as an argument for your Connect class constructor. You can then look at FactoryModuleBuilder so that you can inject dependencies to your Connect class in your code. As for your requirement about singletons, I am not too sure of what you mean there. A singleton means, by definition, that there's only one instance of a class. Here, you want two (or possibly more). What I suspect is that you want at most a single Connect instance per IP String in the entire application. If that is the case, your factory needs to be a bit cleverer that the one created automatically by FactoryModuleBuilder. It could be a singleton itself and store an index (map? concurrent map? cache? It depends on your thread-safety requirements) of ip -> connect instances for those that have been already created.
Hope it helps.

how can i check if a proxy is alive or not in JAVA

I'm creating a 'bot' opening a page with random proxies obtained from a file.txt, but i wanna check first if the proxy is alive or not before using them.
I'm not going to put any code here because I am only doing a request if somebody have a simple method to do this task.
I read about InetSocketAddress but I have the problem that I can only use it with (String Host, Int port). How can I pass Host+port together in a simple String?
If this is not the solution, can someone explain me another method?
This approach is invalid.
The only viable way to test whether any resource is available is to try to use it in the normal course of your program, and handle the errors if it fails.
Otherwise you're involved in predicting the future. It might be up when you test and down when you use. Or the other way around. Or you might test it in a different way from how you will use it, and so, again, get a different result.
I read about "InetSocketAddress" ...
If you are going to test the proxy, this is one approach. (Just test that you can open a TCP/IP connection to the proxy, and then close it). Another way would be to set up your own tiny webserver with a text page, and test the proxy by using it to access your test page. (That gives a more accurate "read" on the liveness of a proxy.)
... but i have the problem that only can i (String Host, Int port) how can i pass Host+port in a simple String?
That is easy. You don't.
You need to parse the string and extract the hostname and the port number. (Java 101 stuff ...) Then pass you pass them as separate arguments.
If the "string" is actually a URL or URI, then you can use java.net.URI to do the parsing.
However, there's another approach. Don't test the proxy. Just use it, and if it doesn't respond then mark it as bad.
IMO, you pretty much have to adopt this approach to make your code robust. If you probe a proxy and find it to be reachable and working at time T, there is no guarantee that it will still be working at time T + 1. No amount of testing will solve that.

When getting ip of computer it shows 0.0.0.0

So I have this code
server = new ServerSocket(6789, 100);
showMessage("Your IP is: "+server.getInetAddress().getHostAddress()+"\n");
and I have also tried .getInetAddress().getCanonicalHostName() and some others, and they all display 0.0.0.0 or 0.0.0.0/0.0.0.0.
Anyone know why this is happening?
Also, when the client connects with the RIGHT ip, it works, it's just displaying it wrong.
0.0.0.0 means it is bound to all adapters, not just one of them like localhost or your network card.
I think you can use one of these approaches -
InetAddress.getLocalHost().getHostAddress()
if you have multiple interfaces you might prefer to use this one -
NetworkInterface.getNetworkInterfaces()
What Peter said. If you want to know about specific local interfaces, look at java.net.NetworkInterface static methods, and from there get a specific InetAddress. Then from there, if you want to use only a specific local interface, you can pass the InetAddress to the ServerSocket constructor.

Java RMI without RMI Registry

I am rather new to java RMI and am trying to create a Peer 2 Peer bit torrent like application wherein multiple instances of the same peer may be on the same machine. This would mean that I would need to be able to have more than one remote object of the same type registered on the same machine. The RMI registry seems to only allow me to have one implementation of a Remote Object on any machine as the registry would not be able to differentiate between which of the objects it should be returning a reference to. Is there a way to bypass the registry such as by specifying an IP and port where I know the other peer is exposing its remote object? If not do you have any ideas how I would be able to create multiple instances of the same object on the same machine? Any help with this would be greatly appreciated...
You can either start multiple rmi registries on different ports, or better bind the instances of the object under different names multiple times. But the best way is probably to do the logic in your code and return a new remote object every time it is needed. E.g. dependent on a parameter:
public MyRemoteObject connect(String name) throws java.rmi.RemoteException {
if("first".equals(name)){
return firstinstance;
}else if("new".equals(name)){
return new MyRemoteObject();
}
...
}
or something like this...
I would suggest you to forget about RMI - IMHO this technique is not applicable for your use case.
Define yourself a network protocol including a serialization and deserialization logic and use this for sending and receiving data on a raw socket connection.

Categories