Announcement Announcement Module
Collapse
No announcement yet.
how start a thread in spring framework? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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

  • #2
    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.

    Comment


    • #3
      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.

      Comment


      • #4
        Originally posted by Costin Leau
        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.MBeanServer FactoryBean"/>

        <bean id="exporter" class="org.springframework.jmx.export.MBeanExporte r">
        <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??

        Comment

        Working...
        X