File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes Convert struts' Action class into stateless session bean ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Convert struts Watch "Convert struts New topic
Author

Convert struts' Action class into stateless session bean ?

Frank Sikuluzu
Ranch Hand

Joined: Dec 16, 2003
Posts: 116
Having a struts application with several action classes. I'm thinking if it is a good idea to convert them into stateless session beans ?


1. currently my action classes are thread safe. Will it still be thread safe if I convert them into stateless session beans ?

2. will it cause performance problem or be overkilling ?
Valentin Tanase
Ranch Hand

Joined: Feb 17, 2005
Posts: 704
Hi Frank,

In my opinion there is a very simple answer to your question: as long as you use struts, you better use the action classes as well. The real question (in my opinion) is this one: do you have business logic implemented inside your action classes? If the answer is yes, then you might consider shifting this logic to some dedicated business logic components. SLSB looks like a good choice to me. And by the way, calling your beans from your struts action classes is a common practice and it will raise neither thread safe nor performance issues. Is just that you need to implement a service locator that caches the home interfaces, etc.
Regards.


I think, therefore I exist -- Rene Descartes
Frank Sikuluzu
Ranch Hand

Joined: Dec 16, 2003
Posts: 116
Originally posted by Valentin Tanase:
Hi Frank,

In my opinion there is a very simple answer to your question: as long as you use struts, you better use the action classes as well. The real question (in my opinion) is this one: do you have business logic implemented inside your action classes? If the answer is yes, then you might consider shifting this logic to some dedicated business logic components. SLSB looks like a good choice to me. And by the way, calling your beans from your struts action classes is a common practice and it will raise neither thread safe nor performance issues. Is just that you need to implement a service locator that caches the home interfaces, etc.
Regards.


"calling your beans from your struts action classes is a common practice". Thanks. Since I am new to EJB and struts subjects. The following question may be stupid --- Is it feasible to directly convert the entire Action class inot a stateless Session bean ? or do I have to take out the business logic from Action class and make it a session bean ?
Valentin Tanase
Ranch Hand

Joined: Feb 17, 2005
Posts: 704

Is it feasible to directly convert the entire Action class inot a stateless Session bean ?

No it is not. There is no way to make an ejb to act as an action class or vice versa.

or do I have to take out the business logic from Action class and make it a session bean ?

Yes this is the way to go. You might use simple POJOs to encapsulate your business logic and use them within your action classes, if your application doesn’t require an ejb container.
Frank Sikuluzu
Ranch Hand

Joined: Dec 16, 2003
Posts: 116
Originally posted by Valentin Tanase:

Yes this is the way to go. You might use simple POJOs to encapsulate your business logic and use them within your action classes, if your application doesn’t require an ejb container.


You said there is no way for a struts "Action" class to be a session bean. Then what if it is not exactly a "struts Action" ? what if I create my own "Action" class which has a method "public void performSOmething(HttpServletRequest, HttpServletResponse) {...}" ? Is there possible to convert this my own action class into a stateless session bean directly ? Theoretically, it is possible that servlet can directly invoke EJB, right ?
So, why do we have to set an extra layer between servlet and stateless bean ? I know we can add such a layer, but want to know if it is OK not to have this layer ?
Valentin Tanase
Ranch Hand

Joined: Feb 17, 2005
Posts: 704
Hi Frank,

So you never give up :-)
Then let me tell you why it�s not possible. Struts action classes implement the org.apache.struts.action.Action interface, which has one method: execute(). Also struts has a configuration files that glues all struts components together. Basically from struts perspective there is only one way to locate an action class: via struts-config.xml. Imagine that you add a config line like this:

Here MyEjbAction is a SLSB that implements the Action interface. You must remark couple of things:
  • Struts cannot locate the MyEjbAction bean. Simply it doesn�t know how to build the initial context, do the lookup, narrow/cast to remote interface, etc.
  • In order for this to compile your MyEjbAction class needs to implement the Action interface. Hence your SLSB must implement this interface. However because struts will eventually call your bean, it can do this only via the remote interface. Therefore the remote interface itself must extend Action as well. As you can see it doesn�t make any sense.
  • Finally you have to understand one very important thing. Action classes are not just another layer of indirection. They do a very precise and important job: they map your HttpRequest to an OO model using ActionForms. It also provides you validation services. Without action classes you�ll have to write spaghetti code throughout your application that gets the httpRequest, extract the input parameters, convert them to the appropriate type, etc. Action classes do this for you out of the box and provide an OOP model to your app, rather than a more procedural one.


  • Regards.
    steve francisco
    Ranch Hand

    Joined: Jan 26, 2005
    Posts: 46
    valentin, my case is bit different from Frank's. I do NOT use struts frame work. I have a central Controller servlet, can I invoke a SLSB from this servlet ? well, I want to pass the HttpServletRequest and HttpServletResponse to that SLSB so that the bean can do its business logic work using the input parameters retrieved from the "HttpServletRequest".
    Valentin Tanase
    Ranch Hand

    Joined: Feb 17, 2005
    Posts: 704
    Hi Steve,


    I have a central Controller servlet, can I invoke a SLSB from this servlet ?

    Of course you can. You can call the SLSB from any other web component, servlet or jsp.

    well, I want to pass the HttpServletRequest and HttpServletResponse to that SLSB so that the bean can do its business logic work using the input parameters retrieved from the "HttpServletRequest".

    That you cannot do it. First of all, from a designer perspective you�ll disqualify yourself; please don�t even think about. Second of all your bean and your web clients are highly coupled. Think that your SLSB is a business component that might be used by java RMI/CORBA clients. Following your approach you�ll compromise this as well. Bottom line is this: please stay away.
    A better approach is transform the HttpRequest/Response classes to VOs and pass/retrieve this to/from your bean. As you might guess this requires some work from your side (you can also check maybe some people did this before and you might get it for free) and here there is where frameworks like struts or spring come to help.
    Regards.
    Frank Sikuluzu
    Ranch Hand

    Joined: Dec 16, 2003
    Posts: 116
    Hi Valentine,

    Thanks for the response. Now I think I am on the same page with you. I will definitely put my EJB in a separate component ! Now, how about web service ? If I turn the business logic into a web service, should I handle the WS in the same way as I handle the SLSB (i.e. put the WS in a separate component) ? I guess so but want to see your opinion.
    Valentin Tanase
    Ranch Hand

    Joined: Feb 17, 2005
    Posts: 704

    If I turn the business logic into a web service, should I handle the WS in the same way as I handle the SLSB (i.e. put the WS in a separate component) ?

    Well, since you�ve asked my opinion... First you might think about this twice. There are lot of implicit middleware service that WS don�t provide the same way ejbs do, transaction and security being the most obvious two. Second of all you might consider performance problems as well. With soap and ws you have to send request/responses via xml and therefore you have to convert your VOs now to xml streams. You have to understand that xml parsing will slow down your app quite a lot. Finally you have to understand that ejbs are very highly available service. Containers pool and cache them, etc. You might also notice that clustering is working better for ejbs. Finally I�ll tell you what I told you before: implement your business tier either through ejbs or pojos. And by the way, next time you better don't ask for my opinion :-)
    Now answering to your question: yes I�ll have the controller calling the appropriate action class that will call apache axis libraries in order to access the WS.
    Regards.
    Roger Chung-Wee
    Ranch Hand

    Joined: Sep 29, 2002
    Posts: 1683
    Now, how about web service ? If I turn the business logic into a web service, should I handle the WS in the same way as I handle the SLSB (i.e. put the WS in a separate component) ?


    You should always be looking to decouple layers. For instance, a client in a presentation tier should know nothing about the business objects in the business tier. How, then, can the client do anything? The solution is for the client to talk to a business delegate (and quite possibly passing a data transfer object as a parameter) which in turn talks to the business object(s). The business delegate pattern is typically implemented as a set of client-side proxies. The client makes a local method call on the business delegate which in turn calls a method of a bean. Only the business delegate knows whether (and how) to communicate with the business tier, be it via RMI, Web Services or whatever.

    Conceptually, this looks as follows (I've omitted other patterns such as session facade and DAO for simplicity):

    Action/servlet <--> business delegate <--> bean <--> data source


    SCJP 1.4, SCWCD 1.3, SCBCD 1.3
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Convert struts' Action class into stateless session bean ?