wood burning stoves 2.0*
The moose likes EJB and other Java EE Technologies and the fly likes Bean Lookup Using Custom Constructor 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 "Bean Lookup Using Custom Constructor" Watch "Bean Lookup Using Custom Constructor" New topic
Author

Bean Lookup Using Custom Constructor

Jeff Storey
Ranch Hand

Joined: Oct 09, 2006
Posts: 118
Hi. I have a stateful bean deployed on a JBoss server, and I look it up using the following code:



The problem is this instantiates the bean using its default constructor. What do I do if want to pass parameters to it when I create it?

Thanks,
Jeff
Sumit Malik
Ranch Hand

Joined: Jan 05, 2007
Posts: 84
Hey Jeff Storey,

I am not very sure whether you can call oveloaded(parameterized) constructor when you are calling a bean through JNDI look up. But i would suggest a workaround to your problem. Lets create a syncronized copy of object(with the properties which you need to pass in parmetrized constructor) and then do pass instance of that object as parameter in your businnes method.

In you business method you can deserialized that instance and get the copy of the properties which you are in need of.
I hope this would help you up......

Cheers !!!
Sumit Malik


If at first you don't succeed, don't try skydiving
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10046
    
163

For a Stateful Session Bean you can have a create method which accepts parameters, in the remote/local home interface. Then when you create the bean using the home interface, you can pass the parameters through the create method.

Something like:



[My Blog] [JavaRanch Journal]
Jeff Storey
Ranch Hand

Joined: Oct 09, 2006
Posts: 118
Thanks for that answer. I don't actually use a create method though, since I am using EJB3, I didn't think I had to...
Sumit Malik
Ranch Hand

Joined: Jan 05, 2007
Posts: 84
Hey Jeff when you are playing with EJB 3.0 then you dont have flexibility of home interface and you are dealing directly with remote/local interface(where actually you have defined you business method).

You can get desired result as Jaikiran Pai has pointed out with overloaded create method in ejb2.x series(because ejb3.0 has backward compatibility) or you can try out with passing parameters in business method(if you are initializing any member variables of ejb through constructor) in reote/local interface.

Cheers !!!
Sumit Malik
Jeff Storey
Ranch Hand

Joined: Oct 09, 2006
Posts: 118
Hi Sumit,

I understand the first suggestion you made about using the create method. I'm a little unclear about the second statement: "try out with passing parameters in business method(if you are initializing any member variables of ejb through constructor) in remote/local interface."

Does that mean to use another method to initialize the parameters (instead of the constructor) or is there a way to actually pass the parameters to the constructor when I am initializing the ejb?

Thanks,
Jeff
Sumit Malik
Ranch Hand

Joined: Jan 05, 2007
Posts: 84
Hi Jeff Storey,

Well, I hope you must be initializing some members variables of ejb through constructor(with parameters) and then you must be manipulating your business logic as per the state of member variables.

I am suggesting just call default constructor(through remote interface) and then call business method from this instance. When you are calling business method(modify the signature of that method by adding the parameters which you are initializing through constructor) then just through the values of those parameters you can define(or manipulate) your business logic.

I hope now you must have got the idea


Cheers !!!
Sumit Malik
Dharmendra Sable
Greenhorn

Joined: May 30, 2006
Posts: 13
Hi Sumit,
But Jeff wants those values to be passed dynamically while lookup, from the calling program. I am not clear about, How he will be able to do it with your suggetion?

Regards,
Dharmendra.
Sumit Malik
Ranch Hand

Joined: Jan 05, 2007
Posts: 84
Hi Dharmendra,

Well, If Jeff is using EJB3.x series then you know we dont have the concept of home interface where we define our create(default or overloaded) methods. If Jeff needs to pass those parameters dynamically then He needs to pass those values(which he is initializing through constructor) in their defined method of business interface(local/remote).

I think this is the solution what he is looking for. Can you suggest some workaround ???

Cheers !!!
Sumit Malik
Dharmendra Sable
Greenhorn

Joined: May 30, 2006
Posts: 13
Sumit,
Now I got what you are saying :-)
I was bit confused. I think Jeff can achieve his objectives with that.

Cheers,
Dharmendra.
Jeff Storey
Ranch Hand

Joined: Oct 09, 2006
Posts: 118
I have been able to achieve the results I needed. I think the fact that I needed to pass custom parameters to a constructor signaled a problem in my design. The old design was not efficiently using EJBs for scalability purposes. I have reworked my design based on your suggestions and now use the default constructor and pass the necessary parameters in the business methods.

Thanks for all of the help.
Jeff
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bean Lookup Using Custom Constructor