Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Opening ServerSocket in EJB

55 views
Skip to first unread message

mk

unread,
Mar 30, 2009, 7:46:08 AM3/30/09
to
Hi all,

Is it possible (and sensible) to open ServerSocket in one of EJB? My web
application (realized in JEE) needs to establish TCP connection with
external application. Data exchange with this application is sometimes
initiated by the application and sometimes by web application on JEE server.
Maybe you have some other suggestions what kind of technology I should
use to establish connetion between client aplication and web application
on JEE server?

Regards
Mariusz

Tom Anderson

unread,
Mar 30, 2009, 2:40:12 PM3/30/09
to
On Mon, 30 Mar 2009, mk wrote:

> Is it possible (and sensible) to open ServerSocket in one of EJB? My web
> application (realized in JEE) needs to establish TCP connection with
> external application. Data exchange with this application is sometimes
> initiated by the application and sometimes by web application on JEE
> server.

I don't know if it will work - i believe the EJB spec bans the use of
Socket and ServerSocket from EJBs. However, there's a vague chance it will
work, and if it does, i imagine opening a server socket right from an EJB
will be the simplest way of solving your problem. However, it's naughty,
since in J2EE, managing sockets and so on is the app server's job. But ...

> Maybe you have some other suggestions what kind of technology I should
> use to establish connetion between client aplication and web application
> on JEE server?

... that's the trouble. I'm not aware of a standard, portable way of
adding application-layer protocols to app servers.

The closest thing i can think of is the J2EE Connector Architecture, one
of the more arcane and mysterious corners of J2EE. You'd write a JCA
resource adaptor to handle the connection, configure an instance of it in
your app server, and use that from an EJB, just as you'd use a database
connection or message queue. Even JCA has no bearing on server sockets,
but i believe that in a resource adaptor, you're allowed to manipulate
them directly, which you can't do from an EJB.

However, i just had a read of the JCA spec, and i got two thirds of the
way through before going stark raving mad, so i'm not sure this is a
productive approach.

You could consider using JMS at the EJB, and then writing a little
standalone server app which would mediate between the external app and a
JMS queue. This is also not pretty, though.

tom

--
The future will accost us with boob-slapping ferocity. -- H. G. Wells

Arved Sandstrom

unread,
Mar 30, 2009, 8:57:45 PM3/30/09
to
Tom Anderson wrote:
> On Mon, 30 Mar 2009, mk wrote:
>
>> Is it possible (and sensible) to open ServerSocket in one of EJB? My
>> web application (realized in JEE) needs to establish TCP connection
>> with external application. Data exchange with this application is
>> sometimes initiated by the application and sometimes by web
>> application on JEE server.
>
> I don't know if it will work - i believe the EJB spec bans the use of
> Socket and ServerSocket from EJBs. However, there's a vague chance it
> will work, and if it does, i imagine opening a server socket right from
> an EJB will be the simplest way of solving your problem. However, it's
> naughty, since in J2EE, managing sockets and so on is the app server's
> job. But ...
[ SNIP ]

Using client sockets in an EJB is OK, using server sockets is not.

From EJB 3.0 Core:

"The EJB architecture allows an enterprise bean instance to be a network
socket client, but it does not allow it to be a network server.
Allowing the instance to become a network server would conflict with
the basic function of the enterprise bean — to serve the EJB clients."

Also:

"An enterprise bean must not attempt to listen on a socket, accept
connections on a socket, or use a socket for multicast."

This is in 21.1.2, Programming Restrictions.

AHS

Arne Vajhøj

unread,
Mar 30, 2009, 9:52:02 PM3/30/09
to

Listening to socket is strictly forbidden in the EJB spec.

You can have the client use:
- EJB Calls
- web service calls

If you really want to use listen to socket, then it
must be done in a JCA adapter.

Arne

Arne Vajhøj

unread,
Mar 30, 2009, 10:09:02 PM3/30/09
to
Tom Anderson wrote:
> On Mon, 30 Mar 2009, mk wrote:
>> Is it possible (and sensible) to open ServerSocket in one of EJB? My
>> web application (realized in JEE) needs to establish TCP connection
>> with external application. Data exchange with this application is
>> sometimes initiated by the application and sometimes by web
>> application on JEE server.
>
> I don't know if it will work - i believe the EJB spec bans the use of
> Socket and ServerSocket from EJBs. However, there's a vague chance it
> will work, and if it does, i imagine opening a server socket right from
> an EJB will be the simplest way of solving your problem. However, it's
> naughty, since in J2EE, managing sockets and so on is the app server's
> job. But ...

I would not be so worried about the management of sockets.

I would be more worried about the management of threads.

Threads being blocked while waiting for connection would
create a mess in thread management.

Arne

Tom Anderson

unread,
Mar 31, 2009, 8:10:20 AM3/31/09
to

True. But you could set a timeout, and then do accepts in a polling way
from a managed thread. Your EJB's API could look like:

boolean checkForConnectionRequest(); // does ServerSocket.accept
DataObject processRequestFromClient(); // does Socket.read

etc. This would be a bit ugly, though.

tom

--
People don't want nice. People want London. -- Al

Lee Fesperman

unread,
Apr 4, 2009, 5:58:29 PM4/4/09
to
On Mar 30, 11:40 am, Tom Anderson <t...@urchin.earth.li> wrote:
> On Mon, 30 Mar 2009, mk wrote:
> > Maybe you have some other suggestions what kind of technology I should
> > use to establish connetion between client aplication and web application
> > on JEE server?
>
> ... that's the trouble. I'm not aware of a standard, portable way of
> adding application-layer protocols to app servers.
>
> The closest thing i can think of is the J2EE Connector Architecture, one
> of the more arcane and mysterious corners of J2EE. You'd write a JCA
> resource adaptor to handle the connection, configure an instance of it in
> your app server, and use that from an EJB, just as you'd use a database
> connection or message queue. Even JCA has no bearing on server sockets,
> but i believe that in a resource adaptor, you're allowed to manipulate
> them directly, which you can't do from an EJB.
>
> However, i just had a read of the JCA spec, and i got two thirds of the
> way through before going stark raving mad, so i'm not sure this is a
> productive approach.

JCA is complex, indeed. For a good overview, try my JDJ article,
"Understanding JCA", at http://java.sys-con.com/node/46283.

--
Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)

Tom Anderson

unread,
Apr 7, 2009, 4:02:32 PM4/7/09
to
On Sat, 4 Apr 2009, Lee Fesperman wrote:

> On Mar 30, 11:40�am, Tom Anderson <t...@urchin.earth.li> wrote:
>> On Mon, 30 Mar 2009, mk wrote:
>>> Maybe you have some other suggestions what kind of technology I should
>>> use to establish connetion between client aplication and web application
>>> on JEE server?
>>
>> ... that's the trouble. I'm not aware of a standard, portable way of
>> adding application-layer protocols to app servers.
>>
>> The closest thing i can think of is the J2EE Connector Architecture, one
>> of the more arcane and mysterious corners of J2EE. You'd write a JCA
>> resource adaptor to handle the connection, configure an instance of it in
>> your app server, and use that from an EJB, just as you'd use a database
>> connection or message queue. Even JCA has no bearing on server sockets,
>> but i believe that in a resource adaptor, you're allowed to manipulate
>> them directly, which you can't do from an EJB.
>>
>> However, i just had a read of the JCA spec, and i got two thirds of the
>> way through before going stark raving mad, so i'm not sure this is a
>> productive approach.
>
> JCA is complex, indeed. For a good overview, try my JDJ article,
> "Understanding JCA", at http://java.sys-con.com/node/46283.

It's a good overview from the client point of view, although a bit heavy
on the enterprisey Big Words and light on code for my taste, but it
doesn't have much to say about implementing resource adaptors.

This guy came to the same conclusion we have about needing to write an RA
to use a server socket, but he then actually did it:

http://itdevworld.wordpress.com/2008/08/26/java-connector-architecture-jca/

Although as far as i can tell he never delivered on his promise to write
about it!

tom

--
You have now found yourself trapped in an incomprehensible maze.

rafaelri

unread,
Apr 7, 2009, 5:28:17 PM4/7/09
to
As Tom said. Yes you need a JCA. Question is: which one? An outbound
or an inbound?
Judging from what I read you need both and they can be deployed on the
same RA.
And in fact that's exactly what we have on our project.
As Tom noticed, I am not that keen on posting code. Even more on this
topic since I wanted to have feedback and different perspectives from
what the spec states.
In regard to the follow up for that post, it isn't linked on the first
one but there it goes (for outbound connectors):
http://itdevworld.wordpress.com/2008/09/01/outbound-jca-connectors-introduction/
http://itdevworld.wordpress.com/2008/09/17/connection-request-flow-on-outbound-jca-connector/
http://itdevworld.wordpress.com/2008/10/06/matchmanagedconnections-short-tip/
http://itdevworld.wordpress.com/2009/02/01/future-updates-to-the-jca-tutorial/

As for inbound connectors I didn't had time to finish this yet but
I'll try to focus again on it on the next weeks. But, still there is
one:

http://itdevworld.wordpress.com/2008/12/24/inbound-jca-connectors-introduction/

As people already warned you here, a resource adapter isn't that easy
to implement, not to mention that there are slight differences between
containers and the spec (as always) isn't that clear on every aspect.

Feel free to post comments there!

regards,
Rafael Ribeiro


On 7 abr, 17:02, Tom Anderson <t...@urchin.earth.li> wrote:
> On Sat, 4 Apr 2009, Lee Fesperman wrote:
> > On Mar 30, 11:40�am, Tom Anderson <t...@urchin.earth.li> wrote:
> >> On Mon, 30 Mar 2009, mk wrote:
> >>> Maybe you have some other suggestions what kind of technology I should
> >>> use to establish connetion between client aplication and web application
> >>> on JEE server?
>
> >> ... that's the trouble. I'm not aware of a standard, portable way of
> >> adding application-layer protocols to app servers.
>
> >> The closest thing i can think of is the J2EE Connector Architecture, one
> >> of the more arcane and mysterious corners of J2EE. You'd write a JCA
> >> resource adaptor to handle the connection, configure an instance of it in
> >> your app server, and use that from an EJB, just as you'd use a database
> >> connection or message queue. Even JCA has no bearing on server sockets,
> >> but i believe that in a resource adaptor, you're allowed to manipulate
> >> them directly, which you can't do from an EJB.
>
> >> However, i just had a read of the JCA spec, and i got two thirds of the
> >> way through before going stark raving mad, so i'm not sure this is a
> >> productive approach.
>
> > JCA is complex, indeed. For a good overview, try my JDJ article,

> > "Understanding JCA", athttp://java.sys-con.com/node/46283.


>
> It's a good overview from the client point of view, although a bit heavy
> on the enterprisey Big Words and light on code for my taste, but it
> doesn't have much to say about implementing resource adaptors.
>
> This guy came to the same conclusion we have about needing to write an RA
> to use a server socket, but he then actually did it:
>

> http://itdevworld.wordpress.com/2008/08/26/java-connector-architectur...

Arne Vajhøj

unread,
Apr 7, 2009, 8:17:25 PM4/7/09
to

JCA for socket comm must have been done a gazillion times.

It is not that unusual.

I even believe some of the more advanced IDE's has wizards to
generate a lot of the JCA stuff.

Arne

Arne Vajhøj

unread,
Apr 7, 2009, 8:22:46 PM4/7/09
to
rafaelri wrote:
> As Tom said. Yes you need a JCA. Question is: which one? An outbound
> or an inbound?
> Judging from what I read you need both and they can be deployed on the
> same RA.

Wouldn't a single adapter with support for bidirectional communication
do fine ?

Arne

0 new messages