5 changed files with 93 additions and 17 deletions
@ -0,0 +1,57 @@ |
|||||||
|
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(); |
||||||
|
} |
||||||
|
|
||||||
|
public 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 void start() { |
||||||
|
if(isInit) { |
||||||
|
super.start(); |
||||||
|
}else { |
||||||
|
throw new RuntimeException("Thread not init yet"); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void close() throws IOException { |
||||||
|
connection.close(); |
||||||
|
connection = null; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue