Service

import java.util.HashMap;
import java.util.Map;

import org.apache.activemq.ActiveMQConnectionFactory;

public class PRDPService {

private Map threadMap = new HashMap();
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();

// Singleton
private final PRDPService instance = new PRDPService();

private PRDPService() {

}

public PRDPService getInstance() {
    return instance;
}

// init-method
public void init() {
    factory = new ActiveMQConnectionFactory();
    String[] clients = new String[] { "PAM_1", "Pru2008" };
    for (String client : clients) {
        MyThread thread = new MyThread(factory, client);
        threadMap.put(client, thread);
    }
}

// destroy-method
public void destroy() {

    for (String clientId : threadMap.keySet()) {
        stop(clientId);
    }
}

private void stop(String clientId) {
    MyThread t = threadMap.get(clientId);
    try {
        if (t.isAlive()) {
            t.doStop();
            t.interrupt();
            t.join();
        }
        threadMap.remove(t);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

// Called in routeBuilder
public void start() {
    for (Thread t : threadMap.values()) {
        t.start();
    }
}

public void start(String clientId) {

    if(isPaused(clientId)){
        resume
    }
}

public boolean isPaused(String clientId) {
    return threadMap.get(clientId).isPaused();
}

public boolean isStopped(String clientId) {
    return threadMap.get(clientId) == null || !threadMap.get(clientId).isAlive();
}

public void pause(String clientId) {
    threadMap.get(clientId).doPause();
}

public void resume(String clientId) {
    threadMap.get(clientId).doResume();
}

}

你可能感兴趣的:(Service)