Simple Java web services - java

Does anyone know of a really simple way of publishing Java methods as web services? I don't really want the overhead of using Tomcat or Jetty or any of the other container frameworks.
Scenario: I've got a set of Java methods in a service type application that I want to access from other machines on the local LAN.

Well, Tomcat or Jetty may be overkill for publishing just some methods as a web service. But on the other hand its not too complicated and they do the job, so why not?
I had a similar problem not too long ago and used a Tomcat together with Axis2. Just download Tomcat, unpack it, deploy the Axis2 WAR. To publish a webservice, there are several aproaches, the one I took is probably one of the easiest:
Just build your application as usual and annotate the web service class and methods with the appropriate annotaions from javax.jws.*. Package everything into a jar. Create a service.xml in the META-INF directory of your jar file and put this into it:
<service name="name of the service" scope="<one of request, session or application>">
<description>
optional description of your service
</description>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</messageReceivers>
<parameter name="ServiceClass" locked="false">put here the fully qualified name of your service class (e.g. x.y.z.FooService)</parameter>
</service>
Rename the .jar to .aar and put it into the /webapps/axis2/WEB-INF/services/ directory. Start tomcat and the service will be deployed. You can check if it is running by visiting the axis2 page (http://localhost:8080/axis2/). There you will see which services are deployed and which methods are exported. Also you can get the WSDL url there to connect to your service.
Read http://ws.apache.org/axis2/1_4_1/contents.html for more about using Axis2. The approach I described here is not found exactly like this in the docs, but it works very well.
Update: If you just want to provide web services and really don't need any of the other features of Tomcat (e.g. serving of plain old web pages, jsps or other stuff), you can also use the Axis2 standalone server. But except for the setup part it doesn't change anything I described.
I've written a slightly more detailed version of this, which can be found at: http://www.slashslash.de/lang/en/2008/10/java-webservices-mit-apache-tomcat-und-axis2/ (don't let the German in URL irritate you, it's written in English)

Web services depend on HTTP. You might not want tomcat or Jetty. In that case, you have to implement HTTP yourself.

Erhm. Why not just use RMI?

Jetty's pretty lightweight. Otherwise, I think XML-RPC is your only sensible option.

The simplier solution than the one that Simon has discribed, ist to use the tools that alrady do that. If you use eclipse you could use http://ws.apache.org/axis2/tools/1_2/eclipse/servicearchiver-plugin.html
to generate the aar file.

Related

How to access configurations specified in XML of a jetty server from code

I've a jetty Server, what I want to configure for HTTPS traffic, I could do this, just simply modified some XML, and ini files (since this IoC is the preferred way), however I would like to access to some of these things which were configured in the xml from other classes, this is in the xml:
<Configure id="sslContextFactory" class="org.eclipse.jetty.util.ssl.SslContextFactory">
<Set name="KeyStoreType">PKCS12</Set>
<Set name="KeyStorePath"><Property name="jetty.base" default="." />/<Property name="jetty.keystore" default="etc/keystore"/></Set>
<Set name="KeyStorePassword"><Property name="jetty.keystore.password" default="OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4"/></Set>
...
What I want is to call the sslContextFactory.getKeyStorePath() function somewhere in my codebase. But where is this sslContextFactory instance, where can I find it, how can I have a reference to it?
I am not entirely clear on what you are asking, but I am going to take a stab at it anyway. It sounds like you are familiar with running Jetty as a distribution and editing Jetty XML and ini files but now want to use some of the Jetty functionality as part of your embedded code?
For SSLContextFactory specifically, the class is included as part of the jetty-util jar file that comes with the distribution. You could also download that jar file by itself on Maven Central.
The official documentation for Jetty includes several examples of embedded Jetty functionality. This page may help you, specifically the example on Multiple Connectors, which uses SSL.
In fact it is not likely to be able to access the jetty standalone server's SSLContextFactory. Jetty by definition can host multiple applications at the same time, and acts like a container for those java web applications.
One option may be to use the embedded jetty server (a solution here), where you can start the jetty server within a java application (e.g. a jar package) and hold the references to your SSLContextFactory instance so that you can reload it whenever you like.
Another option may be using the maven jetty runner, again from another customized application, controlling the SslContextFactory yourself. This I cannot confirm at the moment, bu will update you as soon as I try it.

Deploy Axis2 .aar without Axis2 web application

I would like to know if there's a way to deploy an Axis2 .aar file without having to include it as part of the Axis 2 web application.
I know that my question is short, but there isn't much information that I could provide to direct my question.
Yes, there is.
The axis2 web archive (axis2.war) can be extracted once and never has to be extracted again after that.
Following changes within the axis2 webapp directory are common:
You add modules in 'axis2/WEB-INF/modules/'.
You add libraries in 'axis2/WEB-INF/lib/'.
You change the configuration in 'axis2/WEB-INF/conf/axis2.xml' file.
You deploy your services in 'axis2/WEB-INF/services/'.
Also, there's an option for hot deploys in the services directory. In the axis2.xml file, the folowing line enables this:
<parameter name="hotdeployment">true</parameter>
You can also override the default location of services with the following configuration parameter:
<parameter name="ServicesDirectory">service</parameter>
You can, of course, also make symlinks (unix/linux) instead, but I would not recommend polluting your installation with those.
So, to wrap it up, after your initial deploy of the axis2.war file, you do not need to redeploy it, it can stay, and you can deploy your .aar-files in the services directory. If hot deploy is not enabled (default), you need to restart your tomcat server/reload context manually.
I believe you want to deploy an axis2 web service in an existing application, if so, look at this

Getting the Jetty instance from Spring Web Service

I am creating a web service with spring + jetty + cxf using the following:
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxws:endpoint id="helloWorld" implementor="com.test.EndpointImp" address="http://localhost:9002/test">
</jaxws:endpoint>
This all works as expected and very well. Now I need to "serve" some servlets. Is there anyway I can get to the jetty Server instance that is created for this, so that I can add the servlets? I dont want to create another Jetty instance on another port just for the servlets I need to use.
Any information will be greatly appreciated.
The solution you are looking for is described in this article. The key points (which I also mentioned in my post) are to use org.apache.cxf.transport.servlet.CXFServlet in your web.xml, don't forget to import META-INF/cxf/cxf-servlet.xml (you did so) and also use relative address="/myservice" attribute. In this case CXF routines will not launch embedded Jetty but use this servlet for processing the inbound requests.
Of course a webapp can handle at the same time some WebServices and servlets.
I suppose your web services are in a web application.
Thus you should have a web.xml (in WEB-INF). You can add your servlets declarations in this web.xml.
Jetty should start your webapp.
We can't help you more if you don't give us more details of your project (Maven based or not, how do you launch Jetty, etc...).

how do i wait for service when another service depends on it

i'm about to deploy two different but dependent war-files into a single jboss (AS 4.2.x GA)
One implements some webservices (jax-ws) exposing their interfaces through
a wsdl. The other one is a (say) web frontend using the aformentioned webservices. When I drop both warfiles to $JBOSS_HOME/server/default/deploy at the same time I can see that the first gets deployed (somehow) but the second one gets stuck and the entire jboss is not responding.
When I deploy them one after another, everything is fine (it just works:)
Is there a way to tell jboss that deployment of warfile2.war has to wait
for warfile1.war to finish deployment before starting deployment of warfile2.war?
Is there a way to determine programatically wheter a given 'service' is deployed
and ready?
I'm wonder what happens when both wars are present and jboss is restarted?
Kind regards,
Jay Wee.
To answer your last question first, JBoss will deploy the contents of its deploy directory in alphabetical order. If you drop two WAR files into a running server's deploy directory, the results are unpredictable, but should be safe, so I'm not sure what's going on there.
By the way you describe the dependency, it sounds like when a user uses the frontend WAR, it calls the web service WAR, and that on startup there's no link between the two? Could anyone be trying to use the frontend WAR while the web service WAR is still deploying? Which WAR comes first alphabetically?
As a possible solution, when you have two WAR files that depend on each other, you should consider packing them both into a single EAR file. That way JBoss will deploy them together in a controlled way.
What about implementing a listener in the web frontend waiting for a successfull head request to the wsdl on localhost?
Thats right. backend.war is deployed before frontend.war.
I deployed it on my local jboss and nobody else has acces to it.
What I can see when i debug into jboss is that the frontend accesses the backend wsdl (https://localhost:9999/app/svc?wsdl ) hangs while jboss is not ready.
\at Arne Burmeister: the listener approach doesen't help. the listener is called to early in the process: I can connect to the backendWsdlUrl but backenWsdlUrl.getConnection().getOutputstream() hangs
I think I'll give the ear a try. Is there a good documentation on how
to pack things together in an ear? (skaffman already pointed me into the right direction)
Thanks a lot to all who help so far,
Jan

Developing Web services applications from existing WSDL files

I have done this on Websphere (re: title of this topic) using wsdl2java for generating wsdl to java mapping xml file.
My endpoint is a generic stateless EJB. The code in EJB is generated by traversing the each wsdl and getting the wsdl operation and stuck it in the generated remote EJB interface.
Each EJB method impl is generic and handles all the services the same.
Used instructions on this doc to do this on WAS: http://publib.boulder.ibm.com/infocenter/wasinfo/v7r0/index.jsp?topic=/com.ibm.websphere.base.doc/info/aes/ae/twbs_devwbsjaxrpcwsdl.html
Now, I am asking you all for help if anyone has done something similar in Sun AS 9.1.
Starting from existing WSDL (and xsd) files. Knowing the sole EJB service endpoint implementation for each services are the same, and generating an EAR file (webservices.xml, ejb-jar.xml, etc).
Have struggled with wscompile and alike, but not getting anyware in the same fashion I did for WebSphere.
Thanks for help.
You want to create a WS client which runs under Sun AS? I don't know Sun AS in detail and I don't know the WS libraries it supplies. But you may want to use a public WS library:
Apache Axis 2
Apache CXF
For every library there is documentation which describes how to create a project from WSDL.
You could even use Eclipse to create a project from WSDL for you: File -> New -> Other... -> Web Services -> WSDL. Make sure you have the "WST Web Services" Plugins installed.
I've never used it myself, but I just recently read about the wsdlLocation() attribute of the WebService annotation, which is supposed to map the service to a preexisting WSDL document (not sure if you're even using EJB3, though).

Categories

Resources