Human Being
Human Being

Reputation: 8387

Best way to achieve thread functionality in Spring

I am doing the chat project in Java with Spring 3.2.

Normally in Java I can create a thread like this:

public class Listener extends Thread{
    public void run(){
         while(true){

         }
    }
}

and start the thread by start().

But in Spring 3.x is there any special classes or any special way to achieve the thread functionality?


My requirements:

I have 2 telecom domain servers. In my application I need to initialize the servers to create the protocol.

After the servers are initialized, I need to start two threads to listen the responses from the telecom domain servers.

What I have done was given below:

public class Listener extends Thread{
    public void run(){

       while(true){
           if(ServerOne.protocol != null){
                Message events = ServerOne.protocol.receive();
                   //Others steps to display the message in the view
           }
       }

    }
}

Is it possible to do the java thread functionality with the quartz ?

If possible which one is better ? if no means , what is the reason ?

Hope our stack members will give the better solution.

Upvotes: 2

Views: 7414

Answers (4)

Nereis
Nereis

Reputation: 576

Since Spring 3, you can use @Schedule annotation:

@Service
public class MyTest {
  ...
  @Scheduled(fixedDelay = 10)
  public getCounter() {...}
}

with <context:component-scan base-package="ch/test/mytest"> and <task:annotation-driven/> in the context file

Please refer to this tutorial:http://spring.io/blog/2010/01/05/task-scheduling-simplifications-in-spring-3-0/

Upvotes: 1

kau
kau

Reputation: 372

You could use Spring's ThreadPoolTaskExecutor
You can define your executor as such in you configuration file

<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" destroy- method="shutdown">
  <property name="corePoolSize" value="2" />
  <property name="maxPoolSize" value="2" />
  <property name="queueCapacity" value="10" />
</bean>

<task:annotation-driven executor="taskExecutor" />

In your Listener you could have a method that does all the work in it and annotate this method with the @Async annotation. Ofcourse, Listener should also be Spring managed.

public class Listener{

    @Async
    public void doSomething(){

       while(true){
           if(ServerOne.protocol != null){
                Message events = ServerOne.protocol.receive();
                   //Others steps to display the message in the view
           }
       }

    }
}

Now everytime doSomething is called, a new thread will be created if there are less than corePoolSize number of threads being run by the executor. Once corePoolSize number of threads are created, every subsequent call to doSomething will create a new thread only if there are more than corePoolSize but less than maxPoolSize running (not idle) threads and the thread queue is full. More about pool sizes can be read in the java docs

Note : While using @Async you might encounter the following exception if CGLIB library is not made available in your application.

Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.

To overcome this without having to add CGLIB dependencies, you can create an interface IListener that has doSomething() in it and then have Listener implement IListener

Upvotes: 1

Multithreader
Multithreader

Reputation: 878

Java has what you need. I advise against using Thread manually. You should always use something that manages the threads for you. Please have a look at this: https://stackoverflow.com/a/1800583/2542027

Upvotes: 1

Antoine Prosper
Antoine Prosper

Reputation: 76

Spring's TaskExecutor is a good way to run these threads in a managed environment. You could refer to http://static.springsource.org/spring/docs/3.0.x/reference/scheduling.html for some examples.

Upvotes: 6

Related Questions