如何在项目中使用redis来实现一些需要的功能实例?

阅读: 评论:0

如何在项目中使用redis来实现一些需要的功能实例?

如何在项目中使用redis来实现一些需要的功能实例?

使用Redis来实现申请布控和一些跟数据库中的数据对比

一、需要使用Jedis的工具类去写一些通用的工具类方法

package com.dis;
import org.apachemons.lang3.StringUtils; 
import org.apachemons.lang3.tuple.*;
//import org.apache.log4j.Logger;  
import redis.clients.jedis.Jedis;  
import redis.clients.jedis.JedisPool;  
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Transaction;import java.util.List;  
import java.util.Map;/** * Redis 工具类 */  
public class JedisUtil {//    protected static Logger logger = Logger(JedisUtil.class);  //Redis服务器IP  private static String ADDR_ARRAY = "localhost";  //Redis的端口号  private static int PORT = 6379;  //访问密码  
//    private static String AUTH = "";//可用连接实例的最大数目,默认值为8;  //如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。  private static int MAX_ACTIVE = 20;  //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。  private static int MAX_IDLE = 20;//最小空闲连接数, 默认0private static int MIN_IDLE=5;//等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;  private static int MAX_WAIT = -1;  //超时时间  private static int TIMEOUT = 10000;//连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认trueprivate static boolean BLOCK_WHEN_EXHAUSTED = false;//设置的逐出策略类名, 默认DefaultEvictionPolicy(当连接超过最大空闲时间,或连接数超过最大空闲连接数)private static String EVICTION_POLICY_CLASSNAME="org.apachemons.pool2.impl.DefaultEvictionPolicy";//是否启用pool的jmx管理功能, 默认trueprivate static boolean JMX_ENABLED=true;private static String JMX_NAME_PREFIX="pool";//是否启用后进先出, 默认trueprivate static boolean LIFO=true;//逐出连接的最小空闲时间 默认1800000毫秒(30分钟)private static long MIN_EVICTABLE_IDLE_TIME_MILLIS=1800000L;//对象空闲多久后逐出, 当空闲时间>该值 且 空闲连接>最大空闲数 时直接逐出,不再根据MinEvictableIdleTimeMillis判断  (默认逐出策略)private static long SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS=1800000L;//每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3private static int NUM_TESTS_PER_EVICYION_RUN=5;//在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;  private static boolean TEST_ON_BORROW = false;//在空闲时检查有效性, 默认falseprivate static boolean TEST_WHILEIDLE=false;//逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1private static long TIME_BERWEEN_EVICTION_RUNS_MILLIS=-1;//连接池管理private static JedisPool jedisPool = null;  /** * redis过期时间,以秒为单位 */  public final static int EXRP_HOUR = 60 * 60;            //一小时  public final static int EXRP_DAY = 60 * 60 * 24;        //一天  public final static int EXRP_MONTH = 60 * 60 * 24 * 30; //一个月  /** * 初始化Redis连接池 */  static {  try {  JedisPoolConfig config = new JedisPoolConfig();config.setBlockWhenExhausted(BLOCK_WHEN_EXHAUSTED);config.setEvictionPolicyClassName(EVICTION_POLICY_CLASSNAME); config.setJmxEnabled(JMX_ENABLED);config.setJmxNamePrefix(JMX_NAME_PREFIX);config.setLifo(LIFO);config.setMinEvictableIdleTimeMillis(MIN_EVICTABLE_IDLE_TIME_MILLIS);config.setMinIdle(MIN_IDLE);config.setNumTestsPerEvictionRun(NUM_TESTS_PER_EVICYION_RUN);config.setSoftMinEvictableIdleTimeMillis(SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS);config.setTestOnBorrow(TEST_ON_BORROW);config.setTestWhileIdle(TEST_WHILEIDLE);config.setTimeBetweenEvictionRunsMillis(TIME_BERWEEN_EVICTION_RUNS_MILLIS);config.setMaxTotal(MAX_ACTIVE);  config.setMaxIdle(MAX_IDLE);  config.setMaxWaitMillis(MAX_WAIT);    jedisPool = new JedisPool(config, ADDR_ARRAY, PORT, TIMEOUT);  System.out.println("connect to redis ok ");} catch (Exception e) {  
//            ("First create JedisPool error : " + e);System.out.println("Initialize JedisPool error : " + e);e.printStackTrace();}  }  /** * 销毁连接池* @return void */ public static void end(){if(null != jedisPool){jedisPool.destroy();System.out.println("连接池关闭");}}/** * 同步获取Jedis实例 * @return Jedis */  public static Jedis getJedis() {Jedis jedis = null;  try {    if (jedisPool != null) {    jedis = Resource();   }  } catch (Exception e) {    
//            ("Get jedis error : "+e);  System.out.println("getJedis error : "+e);e.printStackTrace();}return jedis;  }    /** * 释放jedis资源 * * @param jedis */  public static void returnResource(final Jedis jedis) {  if (jedis != null && jedisPool != null) {  jedis.close();
//            System.out.println("返回jedis资源!");}  }  /** * 设置 List * * @param key * @param value */  public static void setListItem(String key, String value) {Jedis resource = null;try {value = StringUtils.isEmpty(value) ? "" : value; resource = getJedis();resource.lpush(key, value);} catch (Exception e) { //            ("Set key error : " + e); System.out.println("setListItem error : " + e); e.printStackTrace(); } finally{if ( resource != null ){returnResource(resource);}}}  /** * 删除 List中的元素 * * @param key * @param value */  public static void RemListItem(String key, String value) {Jedis resource = null;try {value = StringUtils.isEmpty(value) ? "" : value; resource = getJedis();resource.lrem(key, 1, value);} catch (Exception e) { //            ("Set key error : " + e); System.out.println("RemListItem error : " + e);e.printStackTrace(); } finally{if ( resource != null ){returnResource(resource);}}}  /** * 获取List值 * * @param key * @return value */  public static List<String> getListItems(String key) {  Jedis resource = null;List<String> value = null; try {resource = getJedis();//如果列表存在,则取出所有if (ists(key)){long len = resource.llen(key);value = resource.lrange(key, 0, len-1);     }} catch (Exception e) {//          ("Get string error : " + e);System.out.println("getListItems error : " + e);e.printStackTrace();} finally {if ( resource != null ){returnResource(resource);}}return value;  }  /** * 设置 单条Hash表值* * @param key * @param value */  public static void setHashItem(String key, Pair<String, String> value) {Jedis resource = null;try {//String pair_val = Value();//value.setValue(StringUtils.isEmpty(pair_val) ? "" : pair_val); resource = getJedis();resource.hset(key, Key(), Value());} catch (Exception e) { //            ("Set key error : " + e); System.out.println("setHashItem error : " + e); e.printStackTrace();} finally{if ( resource != null ){returnResource(resource);}}}  /** * 检查Key是否存在* @param key * @return value */  public static Boolean ExsitKey(String key) {  Jedis resource = null;Boolean value = false; try {resource = getJedis();if (ists(key) ){   value = true;           }} catch (Exception e) {//          ("Get string error : " + e);System.out.println("ExsitKey error : " + e);e.printStackTrace();} finally {if ( resource != null ){returnResource(resource);}}return value;  }  /** * 设置Key的有效期* * @param key       要设置的key* @param timestamp 设置的有效期限(unix时间戳)*/  public static void setKeyExpireTime(String key, long timestamp) {Jedis resource = null;try {resource = getJedis();pireAt(key, timestamp);} catch (Exception e) { //            ("Set key error : " + e); System.out.println("setKeyExpireTime error : " + e); e.printStackTrace();} finally{if ( resource != null ){returnResource(resource);}}}  /** * 删除Key* * @param key       要删除的key*/  public static void removeKey(String key) {Jedis resource = null;try {resource = getJedis();resource.del(key);            } catch (Exception e) { //            ("Set key error : " + e); System.out.println("removeKey error : " + e); e.printStackTrace();} finally{if ( resource != null ){returnResource(resource);}}}  /** * 检查Hash表中对应的field是否存在* * @param key * @return value */  public static Boolean ExsitHashItem(String key, String field_name) {  Jedis resource = null;Boolean value = false; try {resource = getJedis();if (resource.hexists(key, field_name)){   value = true;           }} catch (Exception e) {//          ("Get string error : " + e);System.out.println("ExsitHashItem error : " + e);e.printStackTrace();} finally {if ( resource != null ){returnResource(resource);}}return value;  }  /** * 获取指定field的值 * * @param key * @return value */  public static String getHashItem(String key, String field_name) {  Jedis resource = null;String value = null; try {resource = getJedis();value = resource.hget(key, field_name);} catch (Exception e) {//          ("Get string error : " + e);System.out.println("getHashItem error : " + e);e.printStackTrace();} finally {if ( resource != null ){returnResource(resource);}}return value;  }  /** * 设置 多条Hash表值* * @param key * @param value */  public static void setHashItems(String key, Map<String, String> value) {Jedis resource = null;try {resource = getJedis();resource.hmset(key, value);} catch (Exception e) { //            ("Set key error : " + e); System.out.println("setHashItems error : " + e); e.printStackTrace();} finally{if ( resource != null ){returnResource(resource);}}}  /** * 获取所有field的值 * * @param key * @return value */  public static Map<String, String> getHashItems(String key) {  Jedis resource = null;Map<String, String> value = null; try {resource = getJedis();value = resource.hgetAll(key);} catch (Exception e) {//          ("Get string error : " + e);System.out.println("getHashItems error : " + e);e.printStackTrace();} finally {if ( resource != null ){returnResource(resource);}}return value;  }  /** * 设置布控信息的事务;* * @param key * @param value */  public static void setEvents(String Listkey, String HashKey, Map<String,String> HashValue, long timestamp) {Jedis resource = null;try {resource = getJedis();Transaction transaction = resource.multi();transaction.hmset(HashKey, HashValue);pireAt(HashKey, timestamp);transaction.lpush(Listkey, HashKey);();} catch (Exception e) { //            ("Set key error : " + e); System.out.println("setEvents error : " + e); e.printStackTrace();} finally{if ( resource != null ){returnResource(resource);}}}  /** * 删除布控信息的事务;* * @param key * @param value */  public static void removeEvents(String Listkey, String HashKey) {Jedis resource = null;try {resource = getJedis();Transaction transaction = resource.multi();transaction.del(HashKey);transaction.lrem(Listkey, 1, HashKey);();} catch (Exception e) { //            ("Set key error : " + e); System.out.println("removeEvents error : " + e); e.printStackTrace();} finally{if ( resource != null ){returnResource(resource);}}}  
}

二、然后是新建一个Redis包在哪个项目的功能模块中使用的时候就直接用spring给注入,然后调用方法就行了,一般都是在controller中去调用这些封装好的Redis功能接口

package com.dis;
import java.util.Date;
SimpleDateFormat;
import org.apachemons.lang3.tuple.*;
import java.util.Map;
import java.util.HashMap;
import java.util.List;public class VehicleInfoCompare {//首次入城判定时间区间(30天) private final static int FirstEntryTimeSection = 30; //车辆过车记录Hash表名称private final static String vehiclePassRecords = "vehiclePassRecords";//车辆过车记录Hash表名称private final static String vehicleFirstEntryRecords = "vehicleFirstEntryRecords";//维稳布控列表名称private final static String StabilityItems = "StabilityItems";//精准车牌布控列表名称private final static String ExactPlateItems = "ExactPlateItems";//模糊车牌布控列表名称private final static String BlurPlateItems = "BlurPlateItems";//车型布控列表名称private final static String VehicleTypeItems = "VehicleTypeItems";//车辆类别布控列表名称private final static String VehicleKindItems = "VehicleKindItems";/** * 得到两个日期时间相差的天数* @param   lastUpdateDate  之前的日期时间(YYYY-MM-DD)* @param   currentDate     之后的日期时间(YYYY-MM-DD)* @return  long    两个日期时间相差的天数*/private static long daysOfTwoDates(String lastUpdateDate, String currentDate) {long days = 0;try {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//跨年不会出现问题//如果时间为:2016-03-18 11:59:59 和 2016-03-19 00:00:01的话差值为 0Date fDate=sdf.parse(lastUpdateDate);Date oDate=sdf.parse(currentDate);days=(Time()-Time())/(1000*3600*24);} catch (Exception e){System.out.println("daysOfTwoDates error : " + e);e.printStackTrace();}return days;}/*** 日期格式字符串转换成时间戳* @param dateStr 字符串日期* @param format   如:yyyy-MM-dd HH:mm:ss* @return long Unix时间戳*/private static long Date2TimeStamp(String dateStr) {try {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");return sdf.parse(dateStr).getTime() / 1000;} catch (Exception e) {System.out.println("Date2TimeStamp error : " + e);e.printStackTrace();}return 0;}/** * 判断时间是否在时间段内 *  * @param date *            当前时间 yyyy-MM-dd HH:mm:ss * @param strDateBegin *            开始时间 00:00:00 * @param strDateEnd *            结束时间 00:05:00 * @return */  private static boolean isInTimeSection(String strDate, String strDateBegin,   String strDateEnd) {  // 截取当前时间时分秒  int strDateH = Integer.parseInt(strDate.substring(11, 13));  int strDateM = Integer.parseInt(strDate.substring(14, 16));  int strDateS = Integer.parseInt(strDate.substring(17, 19));  // 截取开始时间时分秒  int strDateBeginH = Integer.parseInt(strDateBegin.substring(0, 2));  int strDateBeginM = Integer.parseInt(strDateBegin.substring(3, 5));  int strDateBeginS = Integer.parseInt(strDateBegin.substring(6, 8));  // 截取结束时间时分秒  int strDateEndH = Integer.parseInt(strDateEnd.substring(0, 2));  int strDateEndM = Integer.parseInt(strDateEnd.substring(3, 5));  int strDateEndS = Integer.parseInt(strDateEnd.substring(6, 8));  if ((strDateH >= strDateBeginH && strDateH <= strDateEndH)) {  // 当前时间小时数在开始时间和结束时间小时数之间  if (strDateH > strDateBeginH && strDateH < strDateEndH) {  return true;  // 当前时间小时数等于开始时间小时数,分钟数在开始和结束之间  } else if (strDateH == strDateBeginH && strDateM >= strDateBeginM  && strDateM <= strDateEndM) {  return true;  // 当前时间小时数等于开始时间小时数,分钟数等于开始时间分钟数,秒数在开始和结束之间  } else if (strDateH == strDateBeginH && strDateM == strDateBeginM  && strDateS >= strDateBeginS && strDateS <= strDateEndS) {  return true;  }  // 当前时间小时数大等于开始时间小时数,等于结束时间小时数,分钟数小等于结束时间分钟数  else if (strDateH >= strDateBeginH && strDateH == strDateEndH  && strDateM <= strDateEndM) {  return true;  // 当前时间小时数大等于开始时间小时数,等于结束时间小时数,分钟数等于结束时间分钟数,秒数小等于结束时间秒数  } else if (strDateH >= strDateBeginH && strDateH == strDateEndH  && strDateM == strDateEndM && strDateS <= strDateEndS) {  return true;  } else {  return false;  }  } else {  return false;  }  }/** *判断是否为首次入城车辆* @param   key 历史过车记录Hash表* @param   vehicleID   本次过车记录中的车辆ID* @param   captureTime 本次过车记录中的过车时间* @return  Boolean true(判断为首次入城车辆) false(判断为非首次入城车辆)*/private static boolean IsFirstEntryCity(String key, String vehicleID, String captureTime){boolean ret = false;try {//当前传入车牌是否在历史过车记录中,如果存在,则取出该车辆上一次过车时间。if ( JedisUtil.ExsitHashItem(key, vehicleID) ){String lastUpdateTime = HashItem(key, vehicleID);//判断本次过车时间与上一次过车时间相比相差是否超过一定时间(暂定30天),如果超过,则判定为首次入城if (  daysOfTwoDates(lastUpdateTime, captureTime) >= FirstEntryTimeSection ){ret = true;}}//如果在过车历史记录中不存在,则也判定为首次入城。else{ret = true;}} catch ( Exception e) {System.out.println("IsFirstEntryCity error : " + e);e.printStackTrace();} finally {Pair<String, String> value = new ImmutablePair<String, String>(vehicleID, captureTime);JedisUtil.setHashItem(key, value);}return ret;}/** * 更新首次入城车辆记录* @param   key 首次入城记录Hash表* @param   vehicleID   本次过车记录中的车辆ID* @param   captureTime 本次过车记录中的过车时间* @return  void*/private static void UpdateFirstEntryCityRecord(String vehicleID, String captureTime){try {if (IsFirstEntryCity(vehiclePassRecords, vehicleID, captureTime)){Pair<String, String> value =  new ImmutablePair<String, String>(vehicleID, captureTime);JedisUtil.setHashItem(vehicleFirstEntryRecords, value);}} catch ( Exception e ) {System.out.println("UpdateFirstEntryCityRecord error : " + e);e.printStackTrace();}}/** * 比对碰撞分析(精准号牌布控、模糊号牌布控)* @param   vehicleID   本次过车记录中的车辆ID(格式为:车牌颜色-车牌号码)* @param   captureTime 本次过车记录中的过车时间* @param   bayonetID   本次过车记录中的过车卡口ID* @return  Map<String, String> key:布控编号  value:布控方式*/public static Map<String, String> GetAlarmInfoByLicenseNumber(String vehicleID, String captureTime, String bayonetID){long startTime=System.currentTimeMillis();Map<String, String> retval = new HashMap<String, String>();List<String>    listExactPlateItems = null;     //精确号牌布控列表List<String>    listBlurPlateItems = null;      //模糊号牌布控列表try {//1. 判断比较精准号牌布控信息listExactPlateItems = ListItems(ExactPlateItems);if ( listExactPlateItems != null ){//遍历所有的精准号牌布控信息for ( String item : listExactPlateItems ){               //如果列表中的精准号牌信息有效,则进行比较判断if ( JedisUtil.ExsitKey(item) ){Map<String, String> ExactPlateItem = HashItems(item);//取得精准号牌布控信息中的布控卡口列表信息,然后比较当前卡口是否在该列表中String bayonetIDList = ("bayonetID");if ( bayonetIDList != null && bayonetIDList.indexOf(bayonetID) >= 0){//取得布控信息中的布控起始日期,然后判断本次过车时间是否是在布控起始日期之后String BeginDate = ("startdate");                     if ( BeginDate != null && daysOfTwoDates(BeginDate, captureTime.substring(0, 10)) >= 0 ){String BeginTime = ("starttime");String EndTime = ("endtime");//如取得布控信息中的布控起始和终止时间,判断过车时间是不是在该时间区间内,若是,则继续判断车牌号码信息if ( BeginTime != null && EndTime != null && isInTimeSection(captureTime, BeginTime, EndTime)){String vlpList = ("platenumbers");//取得布控信息中的车牌号码列表,判断过车信息中的车牌号码是否在其中,若在,则保存报警信息。if ( vlpList != null && vlpList.indexOf(vehicleID) >= 0 ){//将此条过车警报信息对应的布控信息保存到返回结果之中。retval.put(item, "0");}}}}   }//如果列表中的精准号牌信息无效,则从列表中删除之else{JedisUtil.RemListItem(ExactPlateItems, item);}           }}//2. 判断比较模糊号牌布控信息listBlurPlateItems = ListItems(BlurPlateItems);if (listBlurPlateItems != null){for ( String item : listBlurPlateItems ){   if ( JedisUtil.ExsitKey(item) ){Map<String, String> BlurPlateItem = HashItems(item);//取得精准号牌布控信息中的布控卡口列表信息,然后比较当前卡口是否在该列表中String bayonetIDList = ("bayonetID");if ( bayonetIDList != null && bayonetIDList.indexOf(bayonetID) >= 0){//取得布控信息中的布控起始日期,然后判断本次过车时间是否是在布控起始日期之后String BeginDate = ("startdate");                      if ( BeginDate != null && daysOfTwoDates(BeginDate, captureTime.substring(0, 10)) >= 0 ){String BeginTime = ("starttime");String EndTime = ("endtime");//如取得布控信息中的布控起始和终止时间,判断过车时间是不是在该时间区间内,若是,则继续判断车牌号码信息if ( BeginTime != null && EndTime != null && isInTimeSection(captureTime, BeginTime, EndTime)){String vlpList = ("platenumbers");if ( vlpList == null ){continue;}String vlpinfo[] = vlpList.split("\|");//遍历所有模糊号牌for ( String str : vlpinfo ){//生成正则表达式str = placeAll("\*", "\.\*");//匹配本次过车记录中的车牌号码,若匹配,则将报警信息保存,然后结束本条布控信息的比对。if ( vehicleID.matches(str) ){//将此条过车警报信息对应的布控信息保存到返回结果之中。retval.put(item, "1");break;}}}}}}   }}} catch ( Exception e ){System.out.println("GetAlarmInfoByLicenseNumber error : " + e);e.printStackTrace();}long endTime=System.currentTimeMillis();System.out.println("GetAlarmInfoByLicenseNumber执行时间:"+(endTime-startTime)+"ms");return retval;}/** * 比对碰撞分析(维稳布控、车型布控、车辆类别布控)* @param   vehicleID   本次过车记录中的车辆ID(格式为:车牌颜色-车牌号码)* @param   captureTime 本次过车记录中的过车时间* @param   bayonetID   本次过车记录中的过车卡口ID* @param   vBrand      本次过车记录中的车辆品牌* @param   vSubBrand   本次过车记录中的车辆子品牌* @param   vMode       本次过车记录中的车辆年款* @param   vType       本次过车记录中的车辆 类别* @return  Map<String, String> key:TrapID(布控编号)  value:布控方式*/public static Map<String, String> GetAlarmInfoByOthers(String vehicleID, String captureTime, String bayonetID, int vBrand, int vSubBrand, int vModel, int vType, int hideFace){long startTime=System.currentTimeMillis();Map<String, String> retval = new HashMap<String, String>();List<String>    listStabilityItems = null;      //维稳布控列表List<String>    listVehicleTypeItems = null;    //车辆类型布控列表List<String>    listVehicleKindItems = null;    //车辆类别布控列表try {//1. 根据卡口数据更新首次入城信息UpdateFirstEntryCityRecord(vehicleID, captureTime);//2. 判断比较维稳布控信息listStabilityItems = ListItems(StabilityItems);if (listStabilityItems != null){for ( String item : listStabilityItems ){   if ( JedisUtil.ExsitKey(item) ){Map<String, String> StabilityItem = HashItems(item);//取得精准号牌布控信息中的布控卡口列表信息,然后比较当前卡口是否在该列表中String bayonetIDList = ("bayonetID");if ( bayonetIDList != null && bayonetIDList.indexOf(bayonetID) >= 0){//取得布控信息中的布控起始日期,然后判断本次过车时间是否是在布控起始日期之后String BeginDate = ("startdate");                      if ( BeginDate != null && daysOfTwoDates(BeginDate, captureTime.substring(0, 10)) >= 0 ){String BeginTime = ("starttime");String EndTime = ("endtime");//如取得布控信息中的布控起始和终止时间,判断过车时间是不是在该时间区间内,若是,则继续判断车牌号码信息if ( BeginTime != null && EndTime != null && isInTimeSection(captureTime, BeginTime, EndTime)){String vlpstate = ("platestate");String vlpinfo = ("platenumber");//布控信息存在错误时,则跳过此条布控信息if ( vlpstate == null || vlpinfo == null ){continue;}int pstate = Integer.parseInt(vlpstate);//布控信息中为精确号牌时,比较车牌是否一致,不一致继续处理下一条布控信息if ( 0 == pstate && !vehicleID.equals(vlpinfo) ){continue;}//布控信息中为模糊号牌时,比较车牌是否匹配,不匹配继续处理下一条布控信息if ( 1 == pstate ){String vlpregx = placeAll("\*", "\.\*");if ( !vehicleID.matches(vlpregx) ){continue;}}String ebstr = ("exceptionbehavior");//布控信息存在错误时,则跳过此条布控信息if ( ebstr == null ){continue;}int ebint = Integer.parseInt(ebstr);//布控信息中遮挡面部被设定if ( ebint % 2 == 1 ){//本次过车记录中未遮挡面部的场合,则继续匹配下一条布控信息if ( hideFace != 1){continue;}}//布控信息中首次入城被设定if ( ebint/2 == 1 ){//取得最新判定为首次入城的时间String fectime = HashItem(vehicleFirstEntryRecords, vehicleID);//如果没有判定过首次入城,或者被判定为首次入城的时间早于布控开始日期,则中止,然后继续匹配下一条布控信息if ( (null == fectime) || (daysOfTwoDates(BeginDate, fectime.substring(0, 10)) < 0) ){continue;}}String vbstr = ("vBrand");String vsbstr = ("vSubBrand");String vmstr = ("vModel");//布控信息存在错误时,则跳过此条布控信息if ( vbstr == null || vsbstr == null || vmstr == null ){continue;}int vbint = Integer.parseInt(vbstr);                                                                            int vsbint = Integer.parseInt(vsbstr);                              int vmint = Integer.parseInt(vmstr);//如果布控条件中未设定品牌(子品牌、年款),或设定的品牌(子品牌、年款)与本次过车记录中的数据不相同,则中止,然后继续匹配下一条布控信息if ( (vbint != 0 && vbint != vBrand) || (vsbint != 0 && vsbint != vSubBrand) || (vmint != 0 && vmint != vModel)){continue;}//将此条过车警报信息对应的布控信息保存到返回结果之中。retval.put(item, "2");}}}}   }}//3. 判断比较车辆类型布控信息listVehicleTypeItems = ListItems(VehicleTypeItems);if (listVehicleTypeItems != null){for ( String item : listVehicleTypeItems ){   if ( JedisUtil.ExsitKey(item) ){Map<String, String> VehicleTypeItem = HashItems(item);//取得精准号牌布控信息中的布控卡口列表信息,然后比较当前卡口是否在该列表中String bayonetIDList = ("bayonetID");if ( bayonetIDList != null && bayonetIDList.indexOf(bayonetID) >= 0){//取得布控信息中的布控起始日期,然后判断本次过车时间是否是在布控起始日期之后String BeginDate = ("startdate");                        if ( BeginDate != null && daysOfTwoDates(BeginDate, captureTime.substring(0, 10)) >= 0 ){String BeginTime = ("starttime");String EndTime = ("endtime");//如取得布控信息中的布控起始和终止时间,判断过车时间是不是在该时间区间内,若是,则继续判断车牌号码信息if ( BeginTime != null && EndTime != null && isInTimeSection(captureTime, BeginTime, EndTime)){                               String vbstr = ("vBrand");                                   String vsbstr = ("vSubBrand");                                   String vmstr = ("vModel");//布控信息存在错误时,则跳过此条布控信息if ( vbstr == null || vsbstr == null || vmstr == null ){continue;}int vbint = Integer.parseInt(vbstr);                                    int vsbint = Integer.parseInt(vsbstr);                                  int vmint = Integer.parseInt(vmstr);//如果布控条件中未设定品牌(子品牌、年款),或设定的品牌(子品牌、年款)与本次过车记录中的数据不相同,则中止,然后继续匹配下一条布控信息if ( (vbint != 0 && vbint != vBrand) || (vsbint != 0 && vsbint != vSubBrand) || (vmint != 0 && vmint != vModel)){continue;}String vlpinfo = ("platenumber");//如果设定了模糊车牌信息,则判断本次过车数据中的车牌信息是否匹配布控信息中的模糊车牌if ( vlpinfo != null && vlpinfo.length() > 0 ){String vlpregx = placeAll("\*", "\.\*");if ( ! vehicleID.matches(vlpregx) ){continue;}}//将此条过车警报信息对应的布控信息保存到返回结果之中。retval.put(item, "3");}}}}   }}//4. 判断比较车辆类别布控信息listVehicleKindItems = ListItems(VehicleKindItems);if ( listVehicleKindItems != null ){for ( String item : listVehicleKindItems ){   if ( JedisUtil.ExsitKey(item) ){Map<String, String> VehicleKindItem = HashItems(item);//取得精准号牌布控信息中的布控卡口列表信息,然后比较当前卡口是否在该列表中String bayonetIDList = ("bayonetID");if ( bayonetIDList != null && bayonetIDList.indexOf(bayonetID) >= 0){//取得布控信息中的布控起始日期,然后判断本次过车时间是否是在布控起始日期之后String BeginDate = ("startdate");                        if ( BeginDate != null && daysOfTwoDates(BeginDate, captureTime.substring(0, 10)) >= 0 ){String BeginTime = ("starttime");String EndTime = ("endtime");//如取得布控信息中的布控起始和终止时间,判断过车时间是不是在该时间区间内,若是,则继续判断车牌号码信息if ( BeginTime != null && EndTime != null && isInTimeSection(captureTime, BeginTime, EndTime)){                               String vbstr = ("vBrand");                               String vtypestr = ("vType");//布控信息存在错误时,则跳过此条布控信息if ( vbstr == null || vtypestr == null ){continue;}int vbint = Integer.parseInt(vbstr);                                    int vtypeint = Integer.parseInt(vtypestr);//如果布控条件中未设定品牌或类型,或设定的品牌类型与本次过车记录中的数据不相同,则中止,然后继续匹配下一条布控信息if ( (vbint != 0 && vbint != vBrand) || (vtypeint != 0 && vtypeint != vType)){continue;}String vlpinfo = ("platenumber");//如果设定了模糊车牌信息,则判断本次过车数据中的车牌信息是否匹配布控信息中的模糊车牌if ( vlpinfo != null && vlpinfo.length() > 0 ){String vlpregx = placeAll("\*", "\.\*");if ( ! vehicleID.matches(vlpregx) ){continue;}}//将此条过车警报信息对应的布控信息保存到返回结果之中。retval.put(item, "4");}}}}   }}} catch ( Exception e ){System.out.println("GetAlarmInfoByOthers error : " + e);e.printStackTrace();}long endTime=System.currentTimeMillis();System.out.println("GetAlarmInfoByOthers执行时间:"+(endTime-startTime)+"ms");return retval;}/** * 取得布控信息* @param   trapID  布控编号* @return  Map<String, String> key:布控数据项名称  value:布控数据项值*/public static Map<String, String> GetTrapInfo(String trapID){   Map<String, String> retval = new HashMap<String, String>();try {if ( JedisUtil.ExsitKey(trapID)){retval = HashItems(trapID);}} catch (Exception e) {System.out.println("GetTrapInfo error : " + e);e.printStackTrace();}return retval;}/** * 添加布控信息* @param   trapID      布控编号* @param   trapKind    布控方式* @param   trapInfo    布控详细信息* @return  boolean true:添加成功  false:添加失败*/public static boolean AddTrapInfo(String trapID, String trapKind, Map<String,String> trapInfo){   boolean retval = false;long startTime=System.currentTimeMillis();try {//计算出本次布控到期的时间戳信息String dateinfo = ("enddate");String timeinfo = ("endtime");String datetime = dateinfo + " " + timeinfo;long timestamp = Date2TimeStamp(datetime);String ListKey = null;//精准号牌布控的场合,将布控编码追加到精准号牌布控信息列表中if ( "0" == trapKind ){ListKey = ExactPlateItems;}//模糊号牌布控的场合,将布控编码追加到模糊号牌布控信息列表中else if ( "1" == trapKind){ListKey = BlurPlateItems;}//维稳布控的场合,将布控编码追加到维稳布控信息列表中else if ( "2" == trapKind){ListKey = StabilityItems;}//车型布控的场合,将布控编码追加到车型布控信息列表中else if ( "3" == trapKind){ListKey = VehicleTypeItems;}//车辆类别布控的场合,将布控编码追加到车辆类别布控信息列表中else if ( "4" == trapKind){ListKey = VehicleKindItems;}//执行事务处理(1,设定布控详细信息的Hash表;2,设定Hash表的有效期;3,将Hash表的Key追加到对应的List列表中;JedisUtil.setEvents(ListKey, trapID, trapInfo, timestamp);retval = true;} catch (Exception e) {System.out.println("AddTrapInfo error : " + e);e.printStackTrace();}long endTime=System.currentTimeMillis();System.out.println("AddTrapInfo执行时间:"+(endTime-startTime)+"ms");return retval;}/** * 删除布控信息* @param   trapID  布控编号* @param   trapKind    布控方式* @return  boolean true:删除成功  false:删除失败*/public static boolean RemoveTrapInfo(String trapID, String trapKind){   boolean retval = false;try {       String ListKey = null;//精准号牌布控的场合,将布控编码从精准号牌布控信息列表中删除if ( "0" == trapKind ){ListKey = ExactPlateItems;}//模糊号牌布控的场合,将布控编码从模糊号牌布控信息列表中删除else if ( "1" == trapKind){ListKey = BlurPlateItems;}//维稳布控的场合,将布控编码从维稳布控信息列表中删除else if ( "2" == trapKind){ListKey = StabilityItems;}//车型布控的场合,将布控编码从车型布控信息列表中删除else if ( "3" == trapKind){ListKey = VehicleTypeItems;}//车辆类别布控的场合,将布控编码从车辆类别布控信息列表中删除else if ( "4" == trapKind){ListKey = VehicleKindItems;}veEvents(ListKey, trapID);} catch (Exception e) {System.out.println("RemoveTrapInfo error : " + e);e.printStackTrace();}return retval;}
}

本文发布于:2024-02-04 09:29:50,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170704122254381.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:来实现   实例   功能   项目   如何在
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23