一卡通应用之消息队列的使用

来源:互联网 发布:网络电话卡怎么办理 编辑:程序博客网 时间:2024/04/28 01:54
一卡通应用之消息队列的使用
ActiveMQ


门禁主机 -》DAG -》报警服务器 -》消息队列 -》CMS/客户端


CommonProducer{
private ExecutorService executorService = Executors.newSingleThreadExecutor();
/**
* 异步起线程发送
*/
public void sendAsyNotifyMsg(final String notify){
Runnable task = new Runnable(){
public void run(){
sendNotifyMsg(notify);
}
};
executorService.execute(task);
}

/**
* 发送通知信息
* @param notify protobuf协议消息对象
*/
public void sendNotifyMsg(String notify) {
try {
getMqComponent().send(notify);
log.info("sendNotifyMsg success ...");
        } catch (JMSException e) {
log.info("" + e);
        } catch (NamingException e) {
        log.info("" + e);
   } catch (Exception e){
    log.info("" + e);
        }
}
}


BaseConsumer {


private static final Logger log = LoggerFactory.getLogger(BaseConsumer.class);


  
private MqComponentFactory mqComponentFactory;

private IMqComponent mqComponent;

private String destionName ;


private boolean topicFlag;

private BasicAcceptThread msgMdp;


public IMqComponent getMqComponent() throws JMSException{
if(mqComponent==null){
    mqComponent = mqComponentFactory.createMqComponent(destionName, topicFlag);
    mqComponent.setBizListener(this);
    mqComponent.setMsgConverter(getMessageConverter());
    mqComponent.setMethodName("receiveMsg");
    }
    return mqComponent;

}
/**
* 获取转换处理类
* @author zhangsuichuan 2015-4-27 下午04:49:47
* @return
*/
public abstract MessageConverter getMessageConverter();
/**
* 启动监听器
*/
public void start() {
try {
       getMqComponent().start();
       log.info("component["+destionName+"] has start ");
        } catch (JMSException e) {
        log.error("启动监听器时出现异常", e);       
        }


}

/**
* 停止监听器
*/
public void stop() {
try {
if(mqComponent!=null){
mqComponent.stop();
}
} catch (Exception e) {
log.error("停止监听器时出现异常", e);
}


}
public void receiveMsg(Object message){
if(message instanceof byte[]){
receiveByteMsg((byte[])message);
}else if(message instanceof String){
receiveStringMsg((String)message);
}else{
try {
processMsg(message);
} catch (Exception e) {
e.printStackTrace();
}
}

}
/**
* 消息接收方法,有实现类来实现
* @param notify
*/
public  void receiveStringMsg(String msg){
try {
processMsg(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
public void processMsg(Object msg){
try{
if(msgMdp==null){
log.error("MsgMdp is null");
return;
}
   msgMdp.processMsg(msg);
}catch(Exception e){
log.error("processMsg has Exception:",e);
}
}



public void receiveByteMsg(byte[] msg){
try {
log.debug("receiveMsg byte size:"+msg.length);
//log.debug("receiveMsg byte :"+msg);

String nofify = new String(msg, "UTF-8");

receiveMsg(nofify);

} catch (UnsupportedEncodingException e) {
log.error("receiveMsg has Exception:"+e);
e.printStackTrace();
}
}
}


BasicAcceptThread {

protected int queue_size = 100000;
protected boolean using_queue_cache = true;
protected boolean using_queue_cache_sec = false;
protected boolean isGetting = false;
protected BlockingQueue<Object> queue_cache = new ArrayBlockingQueue<Object>(queue_size);
protected BlockingQueue<Object> queue_cache_sec = new ArrayBlockingQueue<Object>(queue_size);

/**
* 接收信息并处理
*/
public void processMsg(Object msg) throws Exception {
if (msg != null) {
addMessage(msg);
}
}

/**
* 选择闲置的QUEUE来接收MESSAGE
*/
private synchronized void addMessage(Object obj) throws Exception {
if (using_queue_cache) {
if (queue_cache.size() < queue_size) {
queue_cache.put(obj);
}
} else {
if (queue_cache_sec.size() < queue_size) {
queue_cache_sec.put(obj);
}
}
}


public synchronized void clearQueue() {
if (!using_queue_cache) {
queue_cache.clear();
} else {
queue_cache_sec.clear();
}
}

public synchronized void isGetting(boolean bln) {
isGetting = bln;
}

/**
* 获取当前闲置QUEUE
*/
public synchronized int getSizeQueue() {
if (using_queue_cache) {
return queue_cache_sec.size();
} else {
return queue_cache.size();
}
}

/**
* 获取当前 WORK QUEUE
*/
public synchronized BlockingQueue<Object> getQueue() {
if (using_queue_cache) {
return queue_cache_sec;
} else {
return queue_cache;
}
}

/**
* 改变接收MESSAGE的QUEUE
*/
public synchronized void changeAcceptQueue() {
if (isGetting) {
return;
}
using_queue_cache = !using_queue_cache;
using_queue_cache_sec = !using_queue_cache_sec;
}

}
0 0
原创粉丝点击