package org.leolo.rail; import java.io.IOException; import org.apache.activemq.transport.stomp.StompConnection; public abstract class BaseProcessingThread extends Thread implements AutoCloseable{ private boolean isInit = false; protected StompConnection connection; private String configPrefix; public BaseProcessingThread(String configPrefix) throws Exception { this.configPrefix = configPrefix; this.connection = getConnection(); } protected abstract void _init(); @Override public abstract void run(); public void init() { _init(); isInit = true; } private StompConnection getConnection() throws Exception { StompConnection conn = new StompConnection(); conn.open( ConfigurationManager.getInstance().getProperty(configPrefix+".host"), ConfigurationManager.getInstance().getInt(configPrefix+".port") ); conn.connect( ConfigurationManager.getInstance().getProperty(configPrefix+".user"), ConfigurationManager.getInstance().getProperty(configPrefix+".pwd") ); return conn; } @Override public synchronized void start() { if(isInit) { super.start(); }else { throw new RuntimeException("Thread not init yet"); } } @Override public void close() throws IOException { connection.close(); connection = null; } }