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.
Related
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.
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.
In integration tests (JDK 6) I'm trying to catch all outgoing TCP connections and mock them. Looks like I should use java.net.Socket#setSocketImplFactory() method. Works fine at the moment, but I can't understand how I can get an access to original factory, in order to instantiate original JDK-provided SocketImpl class. I need this mostly because I want to let some connections to go out freely, without mocking. Can you suggest some manuals/guidelines/instructions about this problem?
Instead of mocking a Socket I would create a Socket service for the Socket to talk to. This can capture all the data written and reply in any manner you wish. It can be run in the same test and possibly in the same thread.
Looking at the source code of the Socket class, there is no original factory - the constructors check to see if factory is null, and if it is, they just assign impl to be a new PlainSocketImpl().
According to the javadoc, you should be able to use SocketFactory#getDefault() to get the default SocketFactory for your environment.
BTW: You might also want to look at this RFE/bug which declares that SocketImplFactory is basically a dead-end: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4245730
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.
I have a list of services which can be identified by names. Every service has a set of parameters (IP address, port, availability and so on). There are also method which can be applied to the services (close connection, open connection, send a message to the server, check if it is responding and so on).
So, I thought it is a natural approach to create a Server class and represent real servers as objects of this class.
But than I realized that it is not really convenient. For example I have a name of the server (just a string) and I would like to do something with this server. Then I need to have a map which maps name of the server to the object representing this server? It does not seems to be an elegant solution.
What I decided is to have a class containing a set of static methods. And then, for example to use it in the following way: ServerClass.sendMessage("NameOfServer","MyMessage") or for example ServerClass.close("NameOfServer") or ServerClass.getIP("NameOfServer").
Is it a good solution?
An advantage of having a class with various instances is that it provides a kind of type safety. If you have
Server myServer = ServerRepository.getServer("NameOfServer");
if (myServer != null) myServer.sendMessage("MyMessage");
then you know before you send the message if your server name has a typo in it (because your repository can't return a matching message).
Do all of your servers expose the same services, or are there some that are dependent on the server. As an example if you have both FooServers which have a method doFoo() and BarServers with a method doBar() but Foo has no doBar and Bar has no doFoo, then this is likely a bad idea as your ServerClass will potentially expose methods that are meaningless to potential callers. If however you know all of your servers are going to be FooServers than this may be a valid approach as you can centralize common code. I would say be careful that your code remains maintainable and you are not forcing common behavior where it needs to be customized, or you end up adding a multitude of extra arguments to indicate "special cases" where you need to the behavior to vary slightly for one reason or another.