• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Create and manage Beans in Spring programmaticly

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using Spring3.1

My application will have kind of bean-manager.

That manager will be able to retrieve request and on each request I need to create new instance of bean dynamically which will be initiate with it's own unique params.

The params will be retrieved via the request method.

This bean must be alive and work asynchronously. (For example it will listen to jms calls, execute methods by demand and so on..)

Moreover I want to have the option to destroy beans also.

Those bean could be resemble as sessions so when the user log off i will destroy those beans.

I understand that I have to create some kind of bean-list or beans pool and manage it with some kind of manager.

How can I create those beans dynamically and have them remain them alive until I destroy them?


Any idea how could I implement such thing?

* Is my case equal to stateless or statefull in EJB?

thanks,
ray.
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would start with a careful read of section 4.5 of the documentation

http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-factory-scopes
 
ray frid
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
It's not sure about scopes.. what would you think about this idea:

Code:
public void createBean(String beanName, String beanParam)
{
System.out.println("beanName=" + beanName + " beanParam=" + beanParam);
ApplicationContext context = ApplicationContextSingleton.getApplicationContext();
innerContext = new StaticApplicationContext(context);
innerContext.registerSingleton(beanName, FixSessionBean.class);
configureBean(innerContext, beanName, beanParam);

}
Code:
public void configureBean(ApplicationContext innerContext, String beanName, String beanParam)
{
FixSessionBean fixSessionBean = (FixSessionBean) innerContext.getBean(beanName);
fixSessionBean.setMyProperty("32");
}
and If I want to retrieve it in the future ill just use:


Code:
FixSessionBean fixSessionBean = (FixSessionBean) innerContext.getBean(beanName);
FixedSessionBean in the xml:

Code:
<bean id="FixSessionBean" class="com.fixgw.beans.FixSessionBean" />

my question is: if I use StaticApplicationContext to create new beans and manage them in the way I just show could be that in future will it harm performences/memory leaks, etc ?
what do you think about this idea generally?

thanks,
ray.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Code:" Is NOT the CODE tag we have here for posting Code. Just look up a little bit for that Code button and click it to get the CODE tags to post code, so that the code is readable.

Mark
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is not a good idea to try to recreate and rewrite what Spring already has automatically and easily in the Spring Framework. Don't re-invent the wheel. It over complicates things and makes them un-maintainable.

Scope is exactly what you are looking for.

Now, being that you are even thinking that you need some beans to be a different scope suggest to me, that you are new to Spring and that I feel that you should pick up Spring In Action 3rd edition or use the Spring documentation at springsource.org to learn more about beans? It is, in my opinion, very very rare that you need objects/beans to be a different scope. That all Spring beans should be stateless and Singleton for the entire application. Now, ALL is very extreme, and that there are very very rare use cases where you do need a bean created more than once for certain scenarios (Rare Rare scenarios) and that Scope is the answer for those rare rare occasions.

There is never, very very very very very rarely a need to create your own pool of beans.

I had more to say, but I think I made my general point. ;)

Good Luck

Mark
 
Bill Gorder
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ray,

Please Use Code Tags it makes it much easier to read.

I would definitely not do what you posted. StaticApplicationContext and StaticWebApplicationContext are useful for testing but should never be used in production code, See the java doc to verify this fact. Depending whats in your context ( a potentially very large object graph) this could definitely cause memory leaks in many setups.

Now that we are past that.

Why are you not sure about scopes? It looks like your biggest issue is that you need to provide your beans arguments at runtime. This will be hopefully be addressed in Spring 3.2 be sure to up vote/watch the following JIRA
https://jira.springsource.org/browse/SPR-7431

As a workaround in the meantime have a look at what was done here:
http://nurkiewicz.blogspot.com/2010/08/creating-prototype-spring-beans-on.html
 
Bill Gorder
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To Marks point, I am not convinced even that level of complexity is needed. If you explained in a little more detail what it is you are trying to accomplish it is quite possible you can attain what you are looking for without all of that.
 
Bill Gorder
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also it seems you have been busy posting elsewhere. Please if you are going to cross post at least give some time for resolution in one place before starting it up in another or at the very least let people know you have been cross posting. I don't like spending my time potentially answering the same question somebody else has already answered.

http://stackoverflow.com/questions/11383760/create-beans-in-spring-dynamiclly
http://stackoverflow.com/questions/11392948/how-to-getbean-by-name-programmatically
http://stackoverflow.com/questions/11411980/using-staticapplicationcontext-for-managing-beans-dynamically
 
ray frid
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bill,Mark,

First of all I am sorry for the /code tags. next time I will pay attention to this.

second, Bill i am sorry that I have cross posting. just I had few thoughts for this idea while discussing it with different people from diffident forums I am sorry if that made you upset. I will continue this discussion in this forum.

Ill try to give more details about my scenario maybe it will help you to help me.

(btw I have read everything about the scopes. just i am not sure that's the solution for me..)


So I have external protocol. that protocol delivers messages. each message has a 'subscribe' request. every time I get a subscribe request I need to create problematically some kind of session or bean or however you call it that need to work asynchronously (getting jms messages, do some actions and etc...)

now when I get via that protocol unsubscribe message I have to kill the specific session/bean whcih I have created( so it will stop listening to jms messages or do any other actions) .

the thing is that each subscribe message has a different id and this is how ill be able to distinct between the sessions I create or destroy.

This is like a web session. the difference is that i am not using any web platform here. when I get request-message from that protocol I need to run a session. and for each request I get I have to create an individual session (or destroy it when it's unsubscribe).

Do you get this now?

thanks again,
ray.
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ray, have you looked at Spring Integration? Also, subscribing to JMS sounds just like you need a simple Topic and use JMS to register or unregister a listener. I think either JMS out of the box can support this, or Spring Integration can do all this for you.

I just don't want you to go about re-inventing the wheel and adding complexity when there is a simple solution out there. Right now it is just about finding that right/perfect solution. Messaging, Topics, Subscribing and unsubscribing is so common in Message Brokers that it is built into them.

Thanks

Mark
 
ray frid
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark,
Yes JMS is part of my solution. the subscribers are basically those sessions which I will have to manage. but I need to manage them programmaticlly Which means I need the option do dynamically register mdb to a topic as well as unregistered him.

 
ray frid
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have actully found a way of how register those mdb's programaticlly but I am afraid it's gonna kill my memory or do any other probs since I am new to Spring.

Please tell me what you think:

So in order to register Beans programticlly I am doing this:



Now if in the future ill need to do any actions with any of the beans I have created ill need to use the getBean(..) method. but I know that I shouldnt use this method since it's cost in performances. any idea how to bypass it in order to put my hand on any of the beans which I have created dynamically?

thanks.

 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still think a Google search or in the docs might give you a better solution.

But also look at spring-configured.

Mark
 
ray frid
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guess you were right. that turned me into another problem.. I couldnt destroy them.
So I changed my way and went into dynamic proto-type creation. I am using the beanfactory to create my objects.
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ray frid wrote:Guess you were right. that turned me into another problem.. I couldnt destroy them.
So I changed my way and went into dynamic proto-type creation. I am using the beanfactory to create my objects.



BeanFactory? Don't you mean ApplicationContext. BeanFactory is really old Spring way back in Spring 1.x days.

Mark
 
ray frid
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

1. What do you think about this idea?
2. I can switch to application context in order to generate the prototype dynamically.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic