一卡通应用之消息队列的使用
来源:互联网 发布:网络电话卡怎么办理 编辑:程序博客网 时间: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;
}
}
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
- 一卡通应用之消息队列的使用
- ActiveMQ消息队列的使用及应用
- ActiveMQ消息队列的使用及应用
- ActiveMQ消息队列的使用及应用
- SpringBoot应用之消息队列
- 消息队列的使用
- 消息队列的使用
- 消息队列的使用
- JMS应用之消息队列ActiveMQ的基本运用
- ActiveMQ消息队列的单机使用及应用(一)
- 【ActiveMq】ActiveMQ消息队列的使用及应用
- linux消息队列的应用
- ucosii 消息队列的应用
- php消息队列的应用
- 消息队列的应用场景
- [整理]阿里云之消息队列的使用
- 关于消息队列的使用
- .Net消息队列的使用
- ICO图标提取编辑专家2007 V5.0
- C++11 —— 基于区间(range)的 for 循环
- JAVA中基本的类型转换(如String转Int)
- HDU 5733 tetrahedron(计算几何)
- 实现一个函数,打印乘法口诀表,口诀表的行数和列数自己指定,输入9,输出9*9口诀表,输出12,输出12*12的乘法口诀表。
- 一卡通应用之消息队列的使用
- Android异步和同步的区别
- IMS Modify Call (1) send request 发出升级视频请求
- 【9920】打包
- CodeForces 534B Covered Path
- View的相关面试题(待补充)
- iOS中Coredata使用
- Mybatis 面向接口编程
- 【Android】神奇的android:clipChildren属性