Reputation: 117
I have an EJB Stateless Bean that is executed automatically by an Scheduler inside Websphere. My EJB is installed in Websphere. Inside my EJB I would like to make an http request to a webservice exposed by other application (inside de same Server). I tried just using relative path because that is how I usually make requests between applications, but inside my bean I don't know how to get the host name and port to build the URL.
I managed to get the host (ip) by doing this:
InetAddress.getLocalHost().toString();
But I also require the port number. The question is: how to get the host and port number of the application server (Websphere) where my EJB application is installed? Is this possible?
This code is how I try to make the request, but this does not work because I need the full path:
URL url = new URL("/MyOtherAppName/myservice");
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length",
Integer.toString(urlParameters.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches(false);
connection.setDoOutput(true);
-edit 1 ----
I think the approach of user Gas is nice, and makes my application easier to configure.
I followed this guide to implement the URL Provider approach:
Upvotes: 1
Views: 1425
Reputation: 18050
I'd suggest different approach, rather than getting server host and port use the URL resource and then get it in the EJB. It will be more portable across servers and also more flexible in case you ever move your service somewhere else.
See Using URL resources within an application
Define URL via console in app server, and use @Resource
annotation in your EJB like this:
@Resource(name="serviceURL", lookup="url/myurl")
URL myURL;
Upvotes: 5