Back Forum Reply New

how start a thread in spring framework?

hi,
I wanna start a thead when I start up tomcat where I deploy a web service based on spring (version 1.2.8), this thread is used to receive alarm or notification over tcp from other PC. when a alarm or notificaton is received,
the thread deal with it,and transfer to other parts of our system.
Is there a good way to implement this thought??

before I used Jboss, there was a good way to do ,that was using mbean.thanks for any answer

Spring offer some nice abstractions for executing tasks but I don't think you have to use it. Basically you can use some thread pooling library if you want the thread to be configurable or you can just start with a simple daemon thread, configure it as a bean and then use Spring JMX to publish its info in the mbean server.
If you are using a thread pool you can expose that info into JMX so you can tweak on a live system the number of threads or the priority for them.

In Spring you have the freedom to create your own (unmanaged) threads (unlike EJB).

You could use the TaskExecutor abstraction the Spring framework provides. But you also can use the java.util.concurrent.Executor that is part of Java 5 (there also is a backport for the concurrency library for java 1.4).

But if a mbean works for you, I don't see a problem using the same approach under Spring.


Originally Posted by Costin LeauSpring offer some nice abstractions for executing tasks but I don't think you have to use it. Basically you can use some thread pooling library if you want the thread to be configurable or you can just start with a simple daemon thread, configure it as a bean and then use Spring JMX to publish its info in the mbean server.
If you are using a thread pool you can expose that info into JMX so you can tweak on a live system the number of threads or the priority for them.

Can you give more detail!!!!thanks.the following is my way to do it:
1  define a class to start several thread  package a.b.c.d;  public class StartService  {       private int startmark = 0;       public void startThread()      {          theadA.start();          theadB.start();          ......                }
      public void setStartmark(int startmark )
{this.startmark = startmark;startThread();
}
  public int getStartThread()
{
return startThread;
}
  }2  configure xml like this:
lt;beansgt;......
lt;bean id=quot;mbeanServerquot; class=quot;org..jmx.support.MBeanServer  FactoryBeanquot;/gt;
lt;bean id=quot;exporterquot; class=quot;org..jmx.export.MBeanExporte  rquot;gt;   lt;property name=quot;beansquot;gt;     lt;mapgt;       lt;entry key=quot;bean:name=XXXServicequot; value-ref=quot;startServicequot;/gt;     lt;/mapgt;   lt;/propertygt;   lt;property name=quot;serverquot; ref=quot;mbeanServerquot;/gt; lt;/beangt;
lt;bean id=quot;startServicequot; class=quot;a.b.c.d.StartServicequot;gt;   lt;property name=quot;startmarkquot; value=quot;1quot;/gt; lt;/beangt;
.......

lt;/beansgt;

3  start up tomcatthe result is that the service defined by me starts up as soon as tomcat starts upI know this is not good way to deal with my question.Can you provide a better way??
¥
Back Forum Reply New