2012-04-10

CODE - 在 amq 增加新的 consumer & producer

續「CODE - 在 amq 新增加 consumer」這篇, 除了 consumer 外, 還多加新的 producer
以建立 EncodeTS 為例~

  1. src/spring-jms-dev.xml 定義 consumer & producer
    ...
    
    
    
        
    
    
    
    
    
        
    
    ...
    
    
        
         
    
        
            
        
        
            
        
    
     ...
    
  2. 建立 consumer & producer
    1. src/net/sweet/jms/consumer/EncodeTS.java
      package net.sweet.jms.consumer;
      
      import net.sweet.encode.EncodeObject;
      import net.sweet.vlog.util.LoggerHelper;
      import org.apache.activemq.command.ActiveMQMapMessage;
      import org.apache.log4j.Logger;
      
      public class EncodeTS {
          private static Logger log = Logger.getLogger(EncodeTS.class);
          public void onMessage(Object message) {     
           ActiveMQMapMessage msg = (ActiveMQMapMessage) message;
        LoggerHelper loggerHelper = null;
        EncodeObject obj = null;
        try {
         obj = new EncodeObject(msg, log);
         loggerHelper = new LoggerHelper(log, "/home/logs/jmsc/msg/" + obj.getMsgID() + ".log");
         loggerHelper.log("@ EncodeTS consumer ! msgID::::::"+obj.getMsgID());
         loggerHelper.log("bye bye !!");
      //   obj.logInput();
      //   obj.logOutput();
        } catch (Throwable e) {
         loggerHelper.log(e);
        }
        loggerHelper = null;
        msg = null;
        obj = null;
          }
      }
      
    2. src/net/sweet/jms/producer/EncodeTS.java
      package net.sweet.jms.producer;
      
      import java.util.ArrayList;
      import net.sweet.VlogJMSConsumer;
      import net.sweet.encode.EncodeObject;
      import org.apache.log4j.Logger;
      
      public class EncodeTS extends Producer {
       private static Logger log = Logger.getLogger(EncodeTS.class);
       public boolean sendMessage(ArrayList params, EncodeObject obj) {
        boolean result = false;
        try {
         result = super.doSend(params, obj, VlogJMSConsumer.MSG_ENCODE_TS, "encode_ts", template, destination);
        } catch (InterruptedException e) {
         obj.getLogHelper().log("FAIL TO SEND EncodeTS! InterruptedException! maybe timeout! exception msg : " + e.getMessage());
         log.fatal(e, e);
         result = false;
        } catch (Throwable t) {
         obj.getLogHelper().log("FAIL TO SEND EncodeTS! exception msg : " + t.getMessage());
         log.fatal(t, t);
         result = false;
        }
        return result;
       }
      }
      
  3. 增加 src/net/sweet/VlogJMSConsumer.java 內容
    1. private static EncodeTS encodeTSProducer = null;
    2. encodeTSProducer = (EncodeTS)ctx.getBean("encodeTSProducer");
    3. public static final int MSG_ENCODE_TS = 9;
    4. case MSG_ENCODE_TS: return encodeTSProducer.sendMessage(params, obj);
  4. 增加 src/net/sweet/jms/producer/Producer.java 內容
    ...
    case VlogJMSConsumer.MSG_ENCODE_TS: // 2012-04-03 add by trully
    case VlogJMSConsumer.MSG_ENCODE_HD_VIDEO:
    case VlogJMSConsumer.MSG_ENCODE_VIDEO_BY_ON2:
    case VlogJMSConsumer.MSG_ENCODE_IOS_VIDEO:    
    case VlogJMSConsumer.MSG_ENCODE_IOS_AUDIO: {
        EncodeObject cloneObj = (EncodeObject)obj.clone();
        cloneObj.setMsgID(msgID);
        msgInst = cloneObj;
    }
    break;
    ...
    




沒有留言:

張貼留言