利用jgroup做负载

public class JavaGroupBroadcastingManager implements NotificationBus.Consumer {
 
 /**
  * 共享数据:未被激活的时间片信息集合(离线通话信息)
  */
 private static Map<String,TimeSlice> voiceMessageMap = new HashMap<String, TimeSlice>();
 
 /**
  * 共享数据:短信信息
  */
 private static Map<String,SmsCcrParameter> smsMessageIdMap = new HashMap<String,SmsCcrParameter>();

 private static final Log log = LogFactory
   .getLog(JavaGroupBroadcastingManager.class);
 private static final String BUS_NAME = "bus.name";
 private static final String CHANNEL_PROPERTIES = "cache.cluster.properties";
 private NotificationBus bus;
 private static JavaGroupBroadcastingManager manager = null;

 public static JavaGroupBroadcastingManager getInstance(){
  if(manager == null){
   manager = new JavaGroupBroadcastingManager();
   try {
    manager.initialize();
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
  return manager;
 }
 
 /**
  * 初始化成员
  */
 public synchronized void initialize()
   throws Exception {
  Properties properties = new Properties();
  String filePath = System.getProperty("com.sntele.surfing.conf") + File.separator + "ocsgroup.properties";
  log.info(filePath);
  properties.load(new FileInputStream(filePath));
  String channelProperties = properties.getProperty(CHANNEL_PROPERTIES);
  String busName = properties.getProperty(BUS_NAME);
  try {
   bus = new NotificationBus(busName, channelProperties);
   bus.start();
   bus.getChannel().setOpt(Channel.LOCAL, new Boolean(false));
   bus.setConsumer(this);
   log.info("JavaGroups clustering support started successfully");
  } catch (Exception e) {
   throw new Exception("Initialization failed: " + e);
  }
 }

 /**
  * 关闭成员
  */
 public synchronized void finialize() throws Exception {
  bus.stop();
  bus = null;
 }

 /**
  * 发送信息
  */
 public void sendNotification(Serializable serializable) {
  bus.sendNotification(serializable);
  if(serializable instanceof SmsCcrParameter){
   SmsCcrParameter smsCcrParameter = (SmsCcrParameter)serializable;
   if(smsCcrParameter.getMemoryAction() == 0){
    smsMessageIdMap.put(smsCcrParameter.getMessageId(), smsCcrParameter);
   }else{
    smsMessageIdMap.remove(smsCcrParameter.getMessageId());
   }
  }else if(serializable instanceof TimeSlice){
   TimeSlice timeSlice = (TimeSlice)serializable;
   if(timeSlice.getMemoryAction() == 0){
    voiceMessageMap.put(timeSlice.getInitCcrParameter().getCallerNumber(),timeSlice);
   }else{
    TimeSliceControlPool.getInstince().getTimeSliceControl(timeSlice.getInitCcrParameter().getProvinceCode()).removeData(timeSlice.getInitCcrParameter().getCallerNumber());
    voiceMessageMap.remove(timeSlice.getInitCcrParameter().getCallerNumber());
   }
  }
 }

 /**
  * 成员发送信息
  */
 public void handleNotification(Serializable serializable) {
  if(serializable instanceof SmsCcrParameter){
   SmsCcrParameter smsCcrParameter = (SmsCcrParameter)serializable;
   if(smsCcrParameter.getMemoryAction() == 0){
    smsMessageIdMap.put(smsCcrParameter.getMessageId(), smsCcrParameter);
   }else{
    smsMessageIdMap.remove(smsCcrParameter.getMessageId());
   }
  }else if(serializable instanceof TimeSlice){
   TimeSlice timeSlice = (TimeSlice)serializable;
   if(timeSlice.getMemoryAction() == 0){
    voiceMessageMap.put(timeSlice.getInitCcrParameter().getCallerNumber(),timeSlice);
   }else{
    TimeSliceControlPool.getInstince().getTimeSliceControl(timeSlice.getInitCcrParameter().getProvinceCode()).removeData(timeSlice.getInitCcrParameter().getCallerNumber());
    voiceMessageMap.remove(timeSlice.getInitCcrParameter().getCallerNumber());
   }
  }
 }

 /**
  * 成员地址
  */
 public Serializable getCache() {
  if (log.isInfoEnabled()) {
   log.info("成员本地地址: " + bus.getLocalAddress().toString());
  }
  return bus.getLocalAddress();
 }

 /**
  * 新成员加入
  */
 public void memberJoined(Address address) {
  if (log.isInfoEnabled()) {
   log.info("新成员加入:" + address);
  }
  Iterator<?> smsIt = smsMessageIdMap.entrySet().iterator();
  log.info("发送缓存中已经存在的短信信息给新成员:" + smsMessageIdMap.size() + "......start ");
  while (smsIt.hasNext()) {
   Map.Entry entry = (Map.Entry) smsIt.next();
   SmsCcrParameter value = (SmsCcrParameter)entry.getValue();
   sendNotification(value);
  }
  Iterator<?> voiceIt = voiceMessageMap.entrySet().iterator();
  log.info("发送缓存中已经存在的语音信息给新成员:" + voiceMessageMap.size() + "......start ");
  while (voiceIt.hasNext()) {
   Map.Entry entry = (Map.Entry) voiceIt.next();
   TimeSlice value = (TimeSlice)entry.getValue();
   sendNotification(value);
  }
 }

 /**
  * 成员离开
  */
 public void memberLeft(Address address) {
  if (log.isInfoEnabled()) {
   log.info("成员离开:" + address);
  }
  //将检测语音的是否到时的信息放到其他服务器上
  Iterator<?> voiceIt = voiceMessageMap.entrySet().iterator();
  log.info("添加离开成员的语音到时检索信息到通话时间片控制中:" + voiceMessageMap.size() + "......start ");
  while (voiceIt.hasNext()) {
   Map.Entry entry = (Map.Entry) voiceIt.next();
   TimeSlice value = (TimeSlice)entry.getValue();
   TimeSliceControlPool.getInstince().getTimeSliceControl(value.getInitCcrParameter().getProvinceCode()).addTimeSlice(value.getInitCcrParameter().getCallerNumber(),value);
  }
 }
 
 public Map<String, TimeSlice> getVoiceMessageMap() {
  return voiceMessageMap;
 }

 public Map<String, SmsCcrParameter> getSmsMessageIdMap() {
  return smsMessageIdMap;
 }

}

 

测试方法:

JavaGroupBroadcastingManager manager = JavaGroupBroadcastingManager.getInstance();
manager.sendNotification(requestSession);

你可能感兴趣的:(cache)