View Full Version : how start a thread in spring framework?
byronlilu
Aug 28th, 2006, 08:43 AM
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
Costin Leau
Aug 28th, 2006, 09:13 AM
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.
Alarmnummer
Aug 29th, 2006, 12:28 AM
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.
byronlilu
Aug 29th, 2006, 03:31 AM
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.
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:
<beans>
......
<bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactory Bean"/>
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="bean:name=XXXService" value-ref="startService"/>
</map>
</property>
<property name="server" ref="mbeanServer"/>
</bean>
<bean id="startService" class="a.b.c.d.StartService">
<property name="startmark" value="1"/>
</bean>
.......
</beans>
3 start up tomcat
the result is that the service defined by me starts up as soon as tomcat starts up
I know this is not good way to deal with my question.
Can you provide a better way??
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.