Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to start a server and a webservice at the same time?

Reply
Thread Tools

How to start a server and a webservice at the same time?

 
 
Iain Adams
Guest
Posts: n/a
 
      08-09-2011
Hi,

I am building an application that requires both a Server/Client socket
connection to multiple clients AND also exposing multiple webservices.

If I was to deploy my application on a Java container (JBoss/Tomcat
etc) is there a way I can get it to a) start the webservices and b)
start the server (so that it can listen on a specific port for
incoming connections)?

Hope that is enought information.

Thanks,
Iain
 
Reply With Quote
 
 
 
 
Tom Anderson
Guest
Posts: n/a
 
      08-09-2011
On Tue, 9 Aug 2011, Iain Adams wrote:

> I am building an application that requires both a Server/Client socket
> connection to multiple clients AND also exposing multiple webservices.
>
> If I was to deploy my application on a Java container (JBoss/Tomcat etc)
> is there a way I can get it to a) start the webservices and b) start the
> server (so that it can listen on a specific port for incoming
> connections)?


Yes.

For the web services, things are pretty simple. You can create a web
service using JAX-WS, annotating an interface and a class with
@WebService, then mapping the class as a servlet (even though it isn't
really a servlet). Geronmio has a good example:

https://cwiki.apache.org/GMOxDOC20/s...th-jax-ws.html

The container will then take care of binding the HTTP port, supplying
handler threads, and so on.

The raw server socket is marginally trickier.

For a start, the J2EE specification specifically provides that most code
in the container (including servlets and EJBs) might not be allowed to
open a server socket or create a thread (that's supposed to the
container's job!). The only code that is officially allowed to open a
server socket is code which is part of a resource adapter; resource
adapters also get access to thread pools. So, if you want to be
squeaky-clean J2EE compliant, you will need to write a resource adapter; i
have never done it, but my impression is that it is not a fun experience.

However, i think it's usually the case that opening server sockets and
creating threads is actually allowed in all parts of the container (JBoss
has never stopped me doing it, for example). So, your path of least
resistance might be to commit the venial sin of deviation from the spec,
and just set up your server in application code.

I would suggest staging your deviation in a servlet. You can write a
servlet which will never actually handle a request, but whose init method
binds a socket and starts a listener thread. You can then mark that
servlet to be started on startup, by adding a load-on-startup element
inside the servlet element in the web.xml.

I wouldn't suggest doing it in an EJB. Too much weird stuff going on.

tom

--
As Emiliano Zapata supposedly said, "Better to die on your feet than
live on your knees." And years after he died, Marlon Brando played him
in a movie. So just think, if you unionize, Marlon Brando might play
YOU in a movie. Even though he's dead. -- ChrisV82
 
Reply With Quote
 
 
 
 
Arne Vajh°j
Guest
Posts: n/a
 
      08-10-2011
On 8/9/2011 12:38 PM, Iain Adams wrote:
> I am building an application that requires both a Server/Client socket
> connection to multiple clients AND also exposing multiple webservices.
>
> If I was to deploy my application on a Java container (JBoss/Tomcat
> etc) is there a way I can get it to a) start the webservices and b)
> start the server (so that it can listen on a specific port for
> incoming connections)?


If they get deployed as a single unit (like an ear file) then
they should become available at (almost) the same time.

Arne

 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      08-10-2011
On 8/9/2011 6:11 PM, Tom Anderson wrote:
> On Tue, 9 Aug 2011, Iain Adams wrote:
>> I am building an application that requires both a Server/Client socket
>> connection to multiple clients AND also exposing multiple webservices.
>>
>> If I was to deploy my application on a Java container (JBoss/Tomcat
>> etc) is there a way I can get it to a) start the webservices and b)
>> start the server (so that it can listen on a specific port for
>> incoming connections)?

>
> Yes.
>
> For the web services, things are pretty simple. You can create a web
> service using JAX-WS, annotating an interface and a class with
> @WebService, then mapping the class as a servlet (even though it isn't
> really a servlet). Geronmio has a good example:
>
> https://cwiki.apache.org/GMOxDOC20/s...th-jax-ws.html
>
> The container will then take care of binding the HTTP port, supplying
> handler threads, and so on.
>
> The raw server socket is marginally trickier.
>
> For a start, the J2EE specification specifically provides that most code
> in the container (including servlets and EJBs) might not be allowed to
> open a server socket or create a thread (that's supposed to the
> container's job!). The only code that is officially allowed to open a
> server socket is code which is part of a resource adapter; resource
> adapters also get access to thread pools. So, if you want to be
> squeaky-clean J2EE compliant, you will need to write a resource adapter;
> i have never done it, but my impression is that it is not a fun experience.
>
> However, i think it's usually the case that opening server sockets and
> creating threads is actually allowed in all parts of the container
> (JBoss has never stopped me doing it, for example). So, your path of
> least resistance might be to commit the venial sin of deviation from the
> spec, and just set up your server in application code.
>
> I would suggest staging your deviation in a servlet. You can write a
> servlet which will never actually handle a request, but whose init
> method binds a socket and starts a listener thread. You can then mark
> that servlet to be started on startup, by adding a load-on-startup
> element inside the servlet element in the web.xml.


If it is worth doing then it is likely worth doing the
correct way.

Meaning JCA for the socket listening and threading.

Few people have experience with JCA, so it is not so easy to
find good information, but the necessary code is actually not
that advanced.

Arne



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
start a multi-sockets server (a socket/per thread) with different ports but same host zxo102 Python 5 08-15-2006 08:37 AM
Problem! webservice.htc calling nonsecure webservice from a secured ssl https webpage batista ASP .Net 1 01-26-2006 12:05 PM
Local LAN Access - Works for WinXP, No Go For Linux - Same Server,Same Laptop Marc Schwartz Cisco 0 06-18-2005 02:28 PM
WebService on Java server versus WebService on .NET Mr. x Java 2 10-09-2003 11:21 PM



Advertisments