Deploying a Java Web Service on a server - java

I have been asked to create a JAVAX-WS web service which basically performs some basic computation on the input to return the output. I also need to lookup some values from a database.
I am using this book :
Java Web Services: Up and Running
What I've done so far :
1. Created the main java program containing the methods that perform the computations.
2. Used wsgen and wsimport to generate the various artifacts.
3. Used Endpoint to publish the service on localhost.
What I need to do :
I need to get it running on something like a windows server for .NET services. So that it can serve multiple machines.
I know next to nothing about web services and servers, and have just gone through the first chapter of the aforementioned book so far.
From what all resources I've gone through I believe I could use GlassFish, but I don't know if it serves my purposes.
So if anyone could point to some helpful resources for the same, it would be extremely helpful.
P.S : I have no idea about looking up the required values from the database, so please point to some resource for that as well.

Endpoint can also serve requests concurrently, you can enable thread pooling by creating a ThreadPoolExecutor and registering it with the endpoint. See Endpoint.setExecutor(Executor executor)
If you want to deploy your Web Service on GlassFish you should change it into a WAR project, see http://docs.oracle.com/javaee/6/tutorial/doc/bnayl.html

Related

Java Webservice - Expert Know How

I have written an axis2 java webservice. This service works perfektly on a Apache Tomcat
Please let me know if it is possibly to make some tasks in a webservice automatically.
Is there any technical solution available to combine a webservice and a normal java application in one component?
For example the application has some functions which are reachable as webservice and on the other side the application listen for new sockets or do other stuff beside the webservice functions
So it is a application with a Web service inside. .
Thanks for your expert know how. ..
BR
SO is really not meant for recommending tools but what you need is an "embedded servlet container". There are a couple to choose from. The first is Jetty http://www.eclipse.org/jetty/ also take a look at "embedded Tomcat"
Instead of using an embedded servlet container you could keep the whole thing running in tomcat but implement a ServletContextListenter that runs on startup that you could start your socket listeners in.

Communication between rest services hosted on different tomcat servers

I have a scenario where i am hosting different java Rest Services on different Tomcat instances (different machines). These projects running on the tomcats do not have any UI. For simplicity's sake, lets assume that the user will directly enter some URL in the browser (or curl) to avail these services. Now I need this service to be able to talk to (call functions) the services available in the other tomcat instance.
For eg. If TomcatInstance1 gets the call, and all this does is act as a 'router' to the different services, i want it to be able to place the Rest call for the other 'service' available on, say, TomcatInstance2. Is this possible?. If so, how to achieve that? (Tried searching for similar questions on SO, couldnt find any). Are there any online reference for the same?
PS: Hosting the services in the same Tomcat Instance is against the requirement that I'm having.
That is completely possible. You can use (for example) Jersey-client (http://jersey.java.net/) to make the queries to the other RESTful web services in the other Tomcat instances. Only need to define the correct URIs of the end points and query them according the the API exposed and call it (like you were a client from a browser, or curl).
See here a nice example of using Jersey-client to do that: http://www.mkyong.com/webservices/jax-rs/restful-java-client-with-jersey-client/
I would suggest Spring Restful api ( http://static.springsource.org/spring/docs/3.0.0.M3/reference/html/ch18s02.html , http://www.mkyong.com/spring-mvc/spring-3-rest-hello-world-example/ ).
As #emgsilva mentioned the only thing you need to do is to point correct uris between each other.
The beauty of spring restful api is it is simple to use and you don't deal with any serialization - deserialization.

Distributed components lifecycle management

My company has plans to implement a clustered system with a lot of services that will be deployed automatically in different machines and will interconnect with each other (SOA style). Sometimes the services will have interdependencies.
For example:
Service B (application) can be started up only when the Service A (Database) is up and running.
Each service is planned to be run as a different java process, possibly deployed as a WAR (inside dedicated tomcat) or even without web at all.
For now we have all the services in the same WAR and only single tomcat that deploys the WAR.
All the services are defined via Spring and Spring manages dependencies for us.
So I'm asking myself whether exist some frameworks that will help to manage the services in a distributed environment as I've described above?
Thanks in advance
Use ZooKeeper.
Correction, use Netflix's Curator, a framework on top of zookeeper which simplifies the work with it.
Where I work I recently implemented a Coordinator class which has two methods:
waitForDependencies - a synchronic method that checks for the liveness of the current service's dependnecies and blocks the current thread until notified that all dependency services are alive. The liveness check is done by verifying the existence of nodes which are created by the depenedency services, at the end of their initializing process, by calling notifyUp
notifyUp - a synchronic method that notifies the world that the current service that calls that method is alive. The notification is done by creating an ephemeral (temporal, stays alive just as long as the connection in which it was created is alive) node in the zk cluster, which is looked for by other services which depend on it, using waitForDependencies
Netflix released their open source tool - Asgard that manages and deploys instances to a cloud. It is tightly coupled with EC2 (the last time I checked). Depending on whether you deploy to the amazon cloud you might find that useful. I'm unaware if it supports dependencies but it does manage deployments on a distributed environment. Netflix does talk about service dependencies a lot on their blog, so the deployment solution might have a feature to solve for that.
I'm not aware of any other service / framework that does this. If you were to write this on your own I guess you could configure a couple of Jenkins tasks that deploy services. One task can depend on another to simulate the service dependency. Pinging URL endpoints can check if Service A exists before B is deployed.
There's another way to look at this. You would not need to check for dependency if you ensure your services are all running properly. Monitoring tools like Nagios can help here. Troubleshooting faulty services immediately can help you focus on deploying Service A instead of checking your dependencies on each deployment.

What are the specific uses of Java Application Server that cannot be done with web servers?

I am a little confused about the roles of a java application server and its differences from a web server.
I found many sites explaining the same difference between the two but not to my satisfaction.
So please explain me about the two following cases:-
1)App. Server and its difference with web server:
From these two links:
Difference between an application server and a servlet container?
What is the difference between application server and web server?
web server: It handles everything through http protocol by accepting requests from clients and sending
responses to them with the help of its servlet container(e.g Apache Tomcat)
App. Server: An application server supports the whole of JavaEE like JMS,JPA,RPC etc.
Now what I am confused with is that how can I use a lot of JavaEE APIs like JMS,JPA etc. with my Tomcat
by adding their jar files in my web application ?
Does that mean that if I use an appliation server I don't have to add those jar files?(I don't think so)
2)The roles of an appl. server (This is very important to me)
From Wikipedia
http://en.wikipedia.org/wiki/Application_Server
An application server provides services such as security,transaction support etc.
"The term is often used for web servers which support the JavaEE" -- It sounds like if we add the required jar files of JavaEE APIs a web server becomes an appl. server.What about it.
Now my question is how an application server performs the tasks of security control or transaction management by itself ?
E.g. in my web application using Spring framework I am providing security by using spring-security and transaction management by using #Transactional annotation and all those things you know.
So does the appl. server have anything to do with my security or transaction management or it has its own ways ?
Forgive my ignorance.
Using Spring, you're in fact embedding some kind of Java EE container inside your application. But even when using Spring, if you need JTA support (because you need distributed XA transactions), you'll need to use an additional transaction manager. If you need JMS, you'll need to install an additional JMS broker. If you need connection pooling, you'll need to use an additional connection pool. Sometimes it's as simple as adding additional jars to the classpath and properties or XML files. Sometimes it's harder.
A Java EE app server comes with everything bundled. You have less flexibility, but you don't need to install, configure and make everything work by yourself.
When you use the Java EE framework, that is a specification. So the application server, if it is Java EE compliant, needs to implement this. So once it is implemented the specification, then it will address Security,transaction etc because it is mentioned in the spec. So it is a contract. Whereas, in a web server, it will just pull out your static resource. There is no need for handling other stuff.
In case of the Spring framework, the framework knows how to handle transaction, security etc. So particularly the developer need not look into these aspects which are implemented by the Application Server in the other scenario.
how an application server performs the tasks of security control or transaction management by itself
It is rather the specification that address these issues, not the application server. So, the duty of the app server is to implement these.
So, if your application is Java EE compliant, then these areas will be addressed and the implementation would have been done by the app server.
May be this is oversimplification,
A web server is basically a HTTP server serving contents over http protocol. So a web server is simply about serving the contents over http protocol. A typical example would be Apache web server. This is simply a file server.
Now the question is where does the web server gets the contents from ? Possible sources are
Static contents (the contents like images/css etc) which are not generated on request but statically served.
Dynamic contents: Simply put, the contents to be served are generated upon the user request.
For the static contents, the web server does not need anything as it simply reads the file and serves it.
For dynamic contents, the web server might need help of additional components which will generate the contents to be served.
Here the Application Server comes into picture.
Now these additional components referred earlier, might interact with database or some other system etc.
In a web environment where your website is exposed to huge number of users (intended/unintended), you need typical services like transaction/security/concurrency etc. so that the user get expected responses and do not see inconsistencies in the behavior of the application.
An application server has inbuilt abilities to manage transaction/security/concurrency/resource management. generally these are referred as Managed services and environment offered by them is called Managed Environment where these basic services are managed by the application server and programmer does not have be bother for them.
Application Server needs web servers or we can say Web servers use Application server's services to generate dynamic contents.
For example, JBoss uses Tomcat as inbuilt web server. Whereas web logic has its own web server. Tomcat again can be called as application server (in principle) as it also offers managed environment for servlets (it manages concurrency and instance pool of servlets/JSPs ).
Coming your your example of Spring:
An Application server will come inbuilt with transaction/security etc whether you need it or not. The Spring offers a very nice way handling this. Spring has all these things BUT you use what you need. Not just these, but just a Java Web Sever like Tomcat is sufficient to build a full fledged services that needs an application server.

Testing Web Services Consumer

Here are some tools that I have found to test web services consumers:
http://www.soapui.org/
https://wsunit.dev.java.net/
Are there any others? I would prefer testing frameworks that are written in Java or Python.
I have used soapui by a maven plugin. It can create junit-linke reports to be run and analysed like unit tests. This can be easily integrated in continious build, also with the free distribution of soapui.
I've used Web Service Studio.
Web Service Studio is a tool to invoke web methods interactively. The
user can provide a WSDL endpoint. On clicking button Get the tool
fetches the WSDL, generates .NET proxy from the WSDL and displays the
list of methods available. The user can choose any method and provide
the required input parameters. On clicking Invoke the SOAP request is
sent to the server and the response is parsed to display the return
value.
This tool is meant for web service implementers to test their web
services without having to write the client code. This could also be
used to access other web services whose WSDL endpoint is known.
Also the Web Services Explorer in Eclipse which comes as part of the Web Tools Platform.
Through UDDI and WSIL, other applications can discover WSDL documents
and bind with them to execute transactions or perform other business
processes. The Web Services Explorer allows you to explore, import,
and test WSDL documents.
The Grinder is right up your ally with both Java and Python, that handles most web services, (SOAP/REST/CORBA/RMI/JMS/EJB) etc.
http://grinder.sourceforge.net/
You really need to be more specific: What is it that you want to test in your WS-consumer? That it calls the right WS? This looks a bit pointless - WS are a perfect place for mocking whatever may be called - without anything being called.
In order to test the consumer you'd otherwise be writing a Webservice that mocks the original, right? I'd suppose that the communication protocol that goes through the wire is not the clients domain - e.g. it's generated. So the only thing a WS-consumer's client sees is the interface. And there's nothing to test in an interface.
It might be that I completely misunderstood your question - please clarify if I did. I'll revise the answer then.

Categories

Resources