This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes Asynchronous EJB3.1 to act as server socket. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Asynchronous EJB3.1 to act as server socket." Watch "Asynchronous EJB3.1 to act as server socket." New topic
Author

Asynchronous EJB3.1 to act as server socket.

Manoj Kulkarni
Greenhorn

Joined: Apr 30, 2012
Posts: 23
    
    1
I have been reading that as per specs, EJB cannot be socket server listening on port. But I have question specific to EJB 3.1, because as per EJB3 expert group it is absolutely fine to open a serversocket to listen using EJB.
Just give your inputs on the below solution.

I create a stateless asynchronous EJB with NOT_SUPPORTED transaction attribute. In that method I used a method local variable to initiate a server socket.
I now have a while(true) loop which keeps on listening on the socket to read the message and process it and reply back.

What is wrong with this? Because as per my analysis.

There is no problem of passivation, because EJB will always be active in container till while(true) loop is broken and serversocket object is method local, so it does not clash with ejbcontext.
There is no concern of client because it is asynchronous EJB so there is no client waiting for ejb method to complete it's task
There is no problem with transaction or it's timeout because it is NOT_SUPPORTED

Let me know if there are any problems with this design and what are those?
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9947
    
161

Let's put this other way around - Why do you need an EJB to do what you are trying to do? EJBs are typically used when you need the container managed services that EJBs provide by default (for example transactions). You are not using any of those in your example, so why do you want to use a EJB?


[My Blog] [JavaRanch Journal]
Manoj Kulkarni
Greenhorn

Joined: Apr 30, 2012
Posts: 23
    
    1
Because we do not want to initiate any thread within the j2ee container, so do we have any other standard way of invoking threads which can be stopped and started from user defined GUI.
I have explored the option of resource adapter but for resource adapter we need to start and stop using application server console and operations team are not allowed to login into application server.

So suggest a possible solution which gives me control over the threads without having the need to invoke any threads using thread.start() but all that should be managed by the container. I want to host a socket server within the j2ee container.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9947
    
161

Manoj Kulkarni wrote:Because we do not want to initiate any thread within the j2ee container, so do we have any other standard way of invoking threads which can be stopped and stared from JMX console.

You haven't explained what the actual use case is. Let's keep aside all the EJB, Java EE, JMX console and other technical implementation details for now. What exactly are you trying to do?
Manoj Kulkarni
Greenhorn

Joined: Apr 30, 2012
Posts: 23
    
    1
I want to host a socket server within j2ee container, suggest a solution without JCA and resource adapter.
The exact requirement is, there will be multiple socket servers listening on different ports, we will be reading the message from the different clients, and saving it to database and responding on the socket.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9947
    
161

Manoj Kulkarni wrote:suggest a solution without JCA and resource adapter.


Manoj Kulkarni wrote:
I have explored the option of resource adapter but for resource adapter we need to start and stop using application server console and operations team are not allowed to login into application server.



If there's a need to start and stop the resource adapter at runtime, then yes admin console is one such tool. Infact, admin consoles (like the name suggests) are meant exactly for this - to let admins control the server. If you still don't want to use admin consoles for this then irrespective of how you implement the service, you'll have to provide some way to control the lifecycle of that service - even if you use an EJB for that. So how do you plan to do that?
Manoj Kulkarni
Greenhorn

Joined: Apr 30, 2012
Posts: 23
    
    1
The asynchronous EJB that are getting invoked through client, that same client can stop same EJB's why breaking the loop based on boolean flag within the while(true) loop executing on separate thread.
Support team are not application administrators but they will be provided separate GUI to stop and start any processes on need basis and there are already many other processes stopped and started using that console, so socket server should not be deviating from the standard process.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9947
    
161

Manoj Kulkarni wrote:I create a stateless asynchronous EJB with NOT_SUPPORTED transaction attribute. In that method I used a method local variable to initiate a server socket.
I now have a while(true) loop which keeps on listening on the socket to read the message and process it and reply back.

Manoj Kulkarni wrote:The asynchronous EJB that are getting invoked through client, that same client can stop same EJB's why breaking the loop based on boolean flag within the while(true) loop executing on separate thread.

That's not possible because there's no guarantee that the client will end up with the same instance of the stateless bean (infact it won't since that instance is already is in use). Overall, the point really is - you don't need EJBs here.

What you are looking for is asynchronous invocations whose lifecycle can be controlled. Perhaps this will help.
Manoj Kulkarni
Greenhorn

Joined: Apr 30, 2012
Posts: 23
    
    1
Servlets are very specifically an HTTP standard. If you don't want HTTP, you shouldn't use servlets. How true is this statement and can I use servlet for hosting a server socket, because it too listens on port number.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9947
    
161

Manoj Kulkarni wrote:Servlets are very specifically an HTTP standard. If you don't want HTTP, you shouldn't use servlets.

HTTPServlet(s) are specifically for HTTP and yes it's if you don't want HTTP then you shouldn't use them.

Manoj Kulkarni wrote:
How true is this statement and can I use servlet for hosting a server socket, because it too listens on port number.

The JCA resource adapters are meant for this and trying to use something else to workaround one of the other requirements is going to bring in issues of its own. I think a better way to approach this problem would be to figure out a way to expose a service which allows you to stop/start the adapter.
Manoj Kulkarni
Greenhorn

Joined: Apr 30, 2012
Posts: 23
    
    1
I have thaught of this solution, this code snippet is partially part of j2ee_connector specifications.

package com.xyz.adapter;
import java.util.*;
import javax.resource.spi.*;
import javax.resource.spi.work.WorkManager;
// ResourceAdapter JavaBean
public class MyResourceAdapterImpl implements ResourceAdapter {
BootstrapContext bootstrapCtx = null;
public void start(BootstrapContext ctx) {
bootstrapCtx = ctx;
... // other operations
}
... // other methods
}

public class DemoEJB {
MyResourceAdapterImpl myRA = ... // get ResourceAdapter JavaBean using jndi lookup
WorkManager workManager = myRA.bootstrapCtx.getWorkManager();
workManager.startWork(socketlistener);
}

So now when the Resource adapter starts up BootStrapContext is set and we can get workmanager on which we can invoke work threads.
socketlistener is a class which will act as socketserver continously listening in a while(true) loop and it implements Work interface.
Let me know your views on this and if this falls in line with all the java specifications. Which I feel it does but need to confirm.
 
wood burning stoves
 
subject: Asynchronous EJB3.1 to act as server socket.
 
Similar Threads
Proxy Server in Java
Urgent help on Chat Program
"Socket is closed"... why?
Opening socket in stateless asynchronous ejb 3.1 in infinite loop.
server socket and accept call