I have 2 grizzly applications I'd like to run side by side on different URLs on the same server. When I change my url from localhost to api.url.local for local debugging it throws a java.nio.channels.UnresolvedAddressException. Is there something I need to do in order for java to recognize another URL (ie - modify hosts file)? Or am I going in the wrong direction?
I'm currently on Windows but will be deploying to Linux. (If it were up to me, I'd run Linux)
public class Main {
public static final URI BASE_URI = getBaseURI();
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost/").port(9998).build();
}
protected static HttpServer startServer() throws IOException {
ResourceConfig rc = new PackagesResourceConfig("com.my.package.api.resources");
rc.getFeatures()
.put(JSONConfiguration.FEATURE_POJO_MAPPING, true);
return GrizzlyServerFactory.createHttpServer(BASE_URI, rc);
}
public static void main(String[] args) throws IOException {
AnnotationConfigApplicationContext annotationCtx = new AnnotationConfigApplicationContext(Config.class);
HttpServer httpServer = startServer();
System.out.println(String.format("Jersey app started with WADL available at " + "%sapplication.wadl\nHit enter to stop it...", BASE_URI, BASE_URI));
System.in.read();
httpServer.stop();
}
}
Running two different applications under different urls on the same machine with one ip address is quite easy. But can require some setup depending on what you want to do.
My assumption is that you're doing this on your development machine. The biggest issue here is that the two servers require different port numbers.
Server #1
http://localhost:8080/
Server #2
http://localhost:8081/
If you want named instances instead. You will have to update your hosts file to point the named instance you want to your local machine. BUT, you'll still have to do separate ports per application.
Server #1
http://api.url.local:8080/
Server #2
http://api.url.local2:8081/
You will also have to update your tomcat (I'm assuming you're using tocmat) config. In the element, I believe you will have to update the 'name' attribute to be the name of your application's URL.
Friendly warning, if you are using the same IP Address, but different host names, SSL certificates will not work very well. Typically in that sort of setup you will need either a wildcard or unified communications certificate.
When you deploy to production, I would recommend using Apache HTTP Server as a proxy to your two applications and have two separate IP Address' unless you can use a SSL wildcard or a unified communications certificate.
This part of the answer is for if you wanted to run one application under two different URLs.
Proxy your application with a Apache Http Server instance and use mod_proxy to forward your two different server names or ip address' to the same Java instance.
My recommendation though, just run two instances of your application. Don't worry too much about resources, hardware is really cheep and worth a lot less then worrying about how to run one instance for two different sites.
Related
For testing we have a DNS server that will respond with dummy records. Previously we could make java use our DNS server (here it is just using local host) by using:
"-Dsun.net.spi.nameservice.nameservers=127.0.0.1",
"-Dsun.net.spi.nameservice.provider.1=dns,sun",
This no longer works under jdk11.
Is it possible to specify the DNS server to use under jdk11? If so how?
Edit:
I also attempted:
-Djava.naming.provider.url=dns://127.0.0.1
from https://github.com/AdoptOpenJDK/openjdk-jdk11/blob/master/src/jdk.naming.dns/share/classes/com/sun/jndi/dns/DnsContextFactory.java but that did not work either.
Defining an alternative DNS server is already not supported: https://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8134577
For testing you should use an alternative hosts file that could be defined by the -Djdk.net.hosts.file=path_to_alternative_hosts_file JVM option.
For example if you have a file with following content at D:\test\hosts
10.20.30.40 google.com www.google.com
Running this code with -Djdk.net.hosts.file=D://test/hosts
public static void main(String[] args) throws UnknownHostException {
InetAddress address = InetAddress.getByName("google.com");
System.out.println(address);
}
will print:
google.com/10.20.30.40
I am using jax rpc style webservice client and service applications.It is working fine,But when I deployed the client in Google app engine.The client is unable to call my server application.
I have made my Ip address static.
Here is the client application piece of code which is deployed in app engine.
public class HelloWorldClient{
public String main(String name,String field2) throws Exception {
URL url = new URL("http://XXX.XX.9.2X:9997/ws/hello?wsdl");
Here is the server application code which is in my system and published
public static void main(String[] args) {
Endpoint.publish("http://xxx.0.x.1:9997/ws/hello", new HelloWorldImpl());
I have kept my firewall to off state.What is the reason for this strange behaviour.I even couldnot able to see the error in appengine log.
Is this enough that I made my ip static or should i make my ip portforward.Is both the things are same.
I think the problem is that you are not use the google fetch library. This is the only supported way to send outbound request from Google App Engine
This two links will give you some more infos about that
https://cloud.google.com/appengine/docs/standard/java/outbound-requests
Can i use org.apache.http.client.HttpClient in google app engine?
Regards
Michael
I have a locally working JAVA RMI Applicaton(Server & Client).
I have used Eclipse & a plugin(Genady) to write & run these applications.
There are three parts in my project:
1. Server
2. Interface(Common for both client & server)
3. Client
Local deployment, using Eclipse(+plugin), works perfectly. The client uses the common interface(added to the "Build Path") to communicate(to n fro) with the server(which also has the common interface added to its Build Path in eclipse).
And now, I'm planning to test the same system in two different computers, both having Internet connection.
What do I do now?
Should I be installing Apache on one computer(Server) & put the Server+Interface files(class-files) into the web-accessible directory? & then run the Client-files(having both client & interface class files) from another computer(Client)??
Can someone help me configure this? I mean, a step-by-step guide as to what I should be doing in order for the remote deployment to work?
Is using Apache(or any web server application) compulsory?? Any other alternative for my application to work without using a web server(I mean, like direct connection or something?)?
(I feel I've given all the info that is required. But if any more info is needed, please ask.)
This is my final year project & my final demo is coming up soon!
Thank you, in advance!
I don't know what Genady does since I rarely use Eclipse.
Developing RMI applications is very easy anyway.
Pack the server code and run it on the remote machine (the R in RMI)(a.k.a. server host), it should register itself with the Registry which by default uses port 1099 but you can create a registry in any port you like:
MyRemoteInterface stub = (MyRemoteInterface) UnicastRemoteObject.exportObject(server, 0); //I don't know if this is needed anymore, or you could make the "server" extend UnicastRemoteObject
int thePortUsedInTheServer = 1099; //default
Registry registry = Registry.createRegistry(thePortUsedInTheServer);
registry.rebind("Server", stub));
Pack the client code and run it on the client(s) (the M+I in RMI), it should locate the Registry in the remote machine (host+port):
String host = ...; //the host where the server is
int port = ...; //the port used in the server
Registry registry = LocateRegistry.getRegistry(host, port);
MyRemoteInterface stub = (MyRemoteInterface)registry.lookup("Server");
stub.myRemoteMethod(); //call your remote methods
The javadoc should help you out. Nothing else is required, no Apache, etc. Watch out for aggresive anti-virus software, firewalls, and the like. Those are the only real issues after you get the gist of it.
I have to authorise users using its Windows account in a web application.
I use LDAP to validate with the Active Directory if the user/pwd in a specific domain is correct, but I need a list of the available domains because the users can be from differents domains. I try a DNS SRV query to list the ldap servers ( _ldap._tcp ) but I don't get the ssl ldap Active Directory servers.
The host app is in a Unix machine, not Windows.
I can't be sure that this will work since I don't have access to the necessary libraries, but it seems that you'll need to do something like this:
Get hold of the ADSI JARs. This is the really tricky bit. Perhaps this article can help with the initial configuration or you can go with J++ here, or maybe this one from Isocra consulting. If you're hosting your application on Linux and calling into a Windows based AD server, then see section 3 of the first article. In essence you'll be creating some Java proxies onto the ADSI COM object and then calling through them into a remote AD server.
Once that's configured, then this might just do it
public class Main {
public static void main(String args[]) throws Exception {
// The key is not to include any domain in your call apparently
Set domains = (Set) ADsGetObject("WinNT:", IADs.iid);
for (PropertyCache domain: domains) {
System.out.println(domain.getName());
}
}
/**
* #dll.import("activeds", ole)
*/
private static native IUnknown ADsGetObject(String path, _Guid riid);
}
Rather than being a complete answer, this may just get you started in the right direction. However, it does look like it could be very difficult to get working.
This SO answer may also help (it's in C#)
I'm trying to find a way to force any connection attempts a Jar makes to an external IP through my proxy server, which is running on localhost(Also a Java application).
Once the proxy server receives the connection it will open a connection with the external IP and begin routing the IO to and from the client/server.
I've been Googling this for 2 days, and I haven't had any luck, I believe I'm using the wrong terms in my search attempts.
If you've got any ideas, please let me know, I'll try anything.
Thanks in advance. - Sean.
If is that a "real" Proxy the you could specify the proxy to use using java system properties.
You have two alternatives:
Specify the proxy in the command line
Hardcode it into your app
Well you actually have three
Specify a .properties file, and read from there, and set it as System property ( which is pretty much option 2 but more dynamic )
From command line you'll use:
java -Dhttp.proxyHost=localhost -Dhttp.proxyPort=8080 -jar YourJar.jar
With that all the http connections you perform will go through localhost at port 8080
The second is add this at the main method of your program:
public static void main( String [] args ) {
System.setProperty("http.proxyHost", "localhost");
System.setProperty("http.proxyPort", "8080");
.....
}
Which does the same.
Finally loading from myapp.properties
public static void main( String [] args ) {
try { // there are cleaner ways of course
ResorceBundle bundle = ResourceBundle.getBundle("myapp");
System.setProperty("http.proxyHost", bundle.getString("proxy.server"));
System.setProperty("http.proxyPort", bundle.getString("proxy.port"));
} catch( MissingResourceException missingResourceException ){}
....
}
You just have to make sure myapp.properties is available from the classpath
More information about this functionality here
If you are asking about general (NOT HTTP / FTP specific!) proxying of Socket connections, then the simple answer is that it is not supported by Java.
When you configure a proxy for HTTP and FTP traffic, the proxying happens at the application protocol level. The Java-side proxy properties tell the URLConnection layer to connect to your designated proxy rather than the IP address from the URL your application is trying to connect to. The Java Socket level is unaware that this is happening. It just sees a requests to connect to the proxy.
This work because the HTTP and FTP protocols specifically support proxying. For instance, the first 'line' of an HTTP GET request message gives the full URL of the page that the client is requesting. If the GET request goes to a proxy, the proxy can figure out where is has to send it.
Looking at the problem of proxying at the Socket level, the first observation is that the standard Java class libraries don't support this. The second observation is that it is actually unimplementable ... unless you implement this as an alternative transport layer. The reason is that IP and TCP/IP simply do not support the notion of explicitly proxying or relaying messages / streams. And even if you did implement such a transport, it doesn't fit into the standard Socket model.
So, if you are really asking about proxying all of the network traffic for a Java application, this can only be implemented outside of the JVM; i.e. at the network transport level of the JVM's (physical or virtual) host operating system.
If it's HTTP traffic or FTP traffic, you could try the following system properties:
http.proxyHost (default: )
http.proxyPort (default: 80 if http.proxyHost specified)
http.nonProxyHosts (default:
See this link for details:
http://java.sun.com/docs/books/tutorial/networking/urls/_setProxy.html