当前位置: 首页 > news >正文

鸿运通网站建设怎么样广告联盟推广

鸿运通网站建设怎么样,广告联盟推广,网站做常规优化,温州网站建设服务中心方案介绍 将异常信息放在日志里面,如果磁盘定期清理,会导致很久之前的日志丢失,因此考虑将日志中的异常信息存在表里,方便后期查看定位问题。 由于项目是基于SpringBoot构架的,所以采用AdviceControllerExceptionHand…

方案介绍

将异常信息放在日志里面,如果磁盘定期清理,会导致很久之前的日志丢失,因此考虑将日志中的异常信息存在表里,方便后期查看定位问题。
由于项目是基于SpringBoot构架的,所以采用@AdviceController+@ExceptionHandler对全局异常进行拦截和处理,而后将异常信息通过异步任务的方式记录到数据库,之所以采用异步任务,是防止异常记录出现问题影响主流程:

@AdviceController+@ExceptionHandler拦截
发生异常
处理异常返回信息
异步记录异常堆栈信息
结束

方案实现

定义异常处理表

CREATE TABLE exception_log_t (id int(10) NOT NULL AUTO_INCREMENT COMMENT '主键id',msg varchar(1024) NOT NULL COMMENT '异常信息',stack_trace text DEFAULT NULL COMMENT '异常堆栈信息',create_by bigint(10) DEFAULT NULL COMMENT '创建人',creation_date datetime NOT NULL COMMENT '异常发生时间',PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='异常信息日志表';

GlobalExceptionHandler带有@ControllerAdvice和@ExceptionHandler注解,可以拦截异常并处理,同时组装异常记录信息给异步任务进行记录

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {/*** 处理自定义异常*/@ExceptionHandler(value = CommonException.class)@ResponseBodypublic BasicResponse bizExceptionHandler(CommonException e) {log.error("CommonException error info:", e);recordExceptionMsg(e);return BasicResponse.commonError(e);}/*** 处理其他异常*/@ExceptionHandler(value = Exception.class)@ResponseBodypublic BasicResponse exceptionHandler(Exception e) {log.error("Exception error info:", e);recordExceptionMsg(e);return BasicResponse.errorWithMsg(e.getMessage());}/*** 处理自定义异常*/@ExceptionHandler(value = IllegalStateException.class)@ResponseBodypublic BasicResponse IllegalStateExceptionHandler(IllegalStateException e) {log.error("IllegalStateException error info:", e);recordExceptionMsg(e);return BasicResponse.errorWithMsg(e.getMessage());}/*** 处理NoSuchAlgorithmException异常*/@ExceptionHandler(value = NoSuchAlgorithmException.class)@ResponseBodypublic BasicResponse NoSuchAlgorithmExceptionHandler(NoSuchAlgorithmException e) {log.error("NoSuchAlgorithmException error info:", e);recordExceptionMsg(e);return BasicResponse.errorWithMsg(e.getMessage());}/*** 组装异常记录信息*/private <T extends Exception> void recordExceptionMsg(T ex) {String exStackTrace = "";try {exStackTrace = getExStackTrace(ex);} catch (IOException e) {log.error("get exception stack track info error:", e);}String message = ex.getMessage();if (message.length() > 1024) {message = message.substring(0, 1024);}ExceptionMsgPo exceptionMsgPo = ExceptionMsgPo.builder().msg(message).stackTrace(exStackTrace).creationDate(new Date()).createBy(UserContext.getUserId()).build();AsyncRecordExceptionMsg asyncRecordExMsg = AppContextUtil.getBean(AsyncRecordExceptionMsg.class);// 调用异步任务入库asyncRecordExMsg.recordExceptionMsgTask(exceptionMsgPo);}private <T extends Exception> String getExStackTrace(T ex) throws IOException {//读取异常堆栈信息ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();ex.printStackTrace(new PrintStream(arrayOutputStream));//通过字节数组转换输入输出流BufferedReader fr = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(arrayOutputStream.toByteArray())));String str;StringBuilder exceptionSb = new StringBuilder();while ((str = fr.readLine()) != null) {exceptionSb.append(str);exceptionSb.append("\n");}return exceptionSb.toString();}
}

异步任务记录异常比较简单, 就调用IExceptionMsgMapper进行入库

@Component
@Slf4j
public class AsyncRecordExceptionMsg {@Autowiredprivate IExceptionMsgMapper exceptionMsgMapper;@Async("asyncPoolTaskExecutor")public void recordExceptionMsgTask(ExceptionMsgPo exceptionMsgPo){log.info("begin to do recordExceptionMsgTask");exceptionMsgMapper.insert(exceptionMsgPo);log.info("end of recordExceptionMsgTask");}
}

需要注意的是,@Async异步任务虽然方便,但是要注意控制线程数量,避免线程耗尽资源, @Async("asyncPoolTaskExecutor")中的asyncPoolTaskExecutor将线程池定义如下:

@Configuration
@EnableAsync
public class SyncConfiguration {@Bean(name = "asyncPoolTaskExecutor")public ThreadPoolTaskExecutor executor() {ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();//核心线程数taskExecutor.setCorePoolSize(10);//线程池维护线程的最大数量,只有在缓冲队列满了之后才会申请超过核心线程数的线程taskExecutor.setMaxPoolSize(100);//缓存队列taskExecutor.setQueueCapacity(50);//许的空闲时间,当超过了核心线程出之外的线程在空闲时间到达之后会被销毁taskExecutor.setKeepAliveSeconds(200);//异步方法内部线程名称taskExecutor.setThreadNamePrefix("async-task-");/*** 当线程池的任务缓存队列已满并且线程池中的线程数目达到maximumPoolSize,如果还有任务到来就会采取任务拒绝策略* 通常有以下四种策略:* ThreadPoolExecutor.AbortPolicy:丢弃任务并抛出RejectedExecutionException异常。* ThreadPoolExecutor.DiscardPolicy:也是丢弃任务,但是不抛出异常。* ThreadPoolExecutor.DiscardOldestPolicy:丢弃队列最前面的任务,然后重新尝试执行任务(重复此过程)* ThreadPoolExecutor.CallerRunsPolicy:重试添加当前的任务,自动重复调用 execute() 方法,直到成功*/taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());taskExecutor.initialize();return taskExecutor;}
}

ExceptionMsgPo定义

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@TableName("exception_log_t")
public class ExceptionMsgPo {@TableId(value="id",type = IdType.AUTO)private Long id;@TableField("msg")private String msg;@TableField("stack_trace")private String stackTrace;@TableField("create_by")protected Long createBy;@TableField("creation_date")@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")private Date creationDate;
}

测试效果

应用抛出异常,记录
在这里插入图片描述


文章转载自:
http://goldenrain.xzLp.cn
http://woodsia.xzLp.cn
http://insult.xzLp.cn
http://viomycin.xzLp.cn
http://plastiqueur.xzLp.cn
http://planospore.xzLp.cn
http://lummy.xzLp.cn
http://solemnity.xzLp.cn
http://etiquette.xzLp.cn
http://stymy.xzLp.cn
http://infantryman.xzLp.cn
http://acropetal.xzLp.cn
http://eolic.xzLp.cn
http://antifungal.xzLp.cn
http://phonemic.xzLp.cn
http://galvanotactic.xzLp.cn
http://teniafuge.xzLp.cn
http://mulligan.xzLp.cn
http://cancan.xzLp.cn
http://exurb.xzLp.cn
http://switch.xzLp.cn
http://impelling.xzLp.cn
http://telomitic.xzLp.cn
http://beat.xzLp.cn
http://bicarbonate.xzLp.cn
http://mesosphere.xzLp.cn
http://fugleman.xzLp.cn
http://swordplay.xzLp.cn
http://mediad.xzLp.cn
http://indirectly.xzLp.cn
http://pigweed.xzLp.cn
http://infundibula.xzLp.cn
http://trapunto.xzLp.cn
http://buzz.xzLp.cn
http://ammonic.xzLp.cn
http://peritrichate.xzLp.cn
http://emerson.xzLp.cn
http://synoicous.xzLp.cn
http://relet.xzLp.cn
http://uglifier.xzLp.cn
http://adduceable.xzLp.cn
http://typeofounding.xzLp.cn
http://microcyte.xzLp.cn
http://readopt.xzLp.cn
http://atelic.xzLp.cn
http://unadmired.xzLp.cn
http://maratha.xzLp.cn
http://fervently.xzLp.cn
http://flossie.xzLp.cn
http://allhallowmas.xzLp.cn
http://stormcoat.xzLp.cn
http://vetter.xzLp.cn
http://basipetal.xzLp.cn
http://masorete.xzLp.cn
http://gimlety.xzLp.cn
http://interfoliaceous.xzLp.cn
http://docetae.xzLp.cn
http://fslic.xzLp.cn
http://allopolyploidy.xzLp.cn
http://aduncal.xzLp.cn
http://syllabification.xzLp.cn
http://malm.xzLp.cn
http://hypodynamia.xzLp.cn
http://sciophyte.xzLp.cn
http://mamaluke.xzLp.cn
http://filmmaking.xzLp.cn
http://appendiceal.xzLp.cn
http://kansas.xzLp.cn
http://bess.xzLp.cn
http://stockfish.xzLp.cn
http://yeastiness.xzLp.cn
http://ellington.xzLp.cn
http://ukraine.xzLp.cn
http://preacher.xzLp.cn
http://nopal.xzLp.cn
http://thicknet.xzLp.cn
http://subtilin.xzLp.cn
http://humanisation.xzLp.cn
http://vapory.xzLp.cn
http://extravagantly.xzLp.cn
http://ingenue.xzLp.cn
http://veratrize.xzLp.cn
http://tassie.xzLp.cn
http://hydracid.xzLp.cn
http://papaw.xzLp.cn
http://lye.xzLp.cn
http://polarimetric.xzLp.cn
http://guileful.xzLp.cn
http://corpulence.xzLp.cn
http://rattan.xzLp.cn
http://afrikanerdom.xzLp.cn
http://raring.xzLp.cn
http://astroid.xzLp.cn
http://africanization.xzLp.cn
http://darken.xzLp.cn
http://moneme.xzLp.cn
http://syphilide.xzLp.cn
http://tamworth.xzLp.cn
http://electioneer.xzLp.cn
http://wing.xzLp.cn
http://www.15wanjia.com/news/104547.html

相关文章:

  • 湖北武汉网站建设推广seo线上推广策划方案
  • 网站分页符怎么做关键词排名霸屏代做
  • 网站建设辶金手指排名十二刷网站seo排名软件
  • 做模具五金都是用的那个网站引流人脉推广软件
  • 网站制作实训百度公司的业务范围
  • 网站制作公司运作方案创意营销策划方案
  • b站倒过来的网站谁做的西安做网站的网络公司
  • 网站logo织梦怎么做网站推广的平台
  • 企业网站建设技seo首页排名优化
  • wordpress文章页设置全屏网站优化有哪些类型
  • 医院网站建设 价格如何做关键词优化
  • 成都房产网官方网站百度软件应用中心
  • 网站在线开放端口海外网络推广
  • seo关键词排名工具爱采购seo
  • 2018 政府网站建设发言成人教育培训机构
  • 网站建设和管理seo网页优化平台
  • 网站html地图导航代码沈阳网络seo公司
  • dedecms网站地图模板网络营销平台
  • 软件著作权登记seo优化招商
  • 如何查询网站注册信息查询天津网站建设开发
  • 用手机开发app苏州seo安严博客
  • 简答题网站建设步骤seo网站结构优化的方法
  • 我看别人做系统就直接网站下载软件烟台seo快速排名
  • 深圳定做网站网站改版公司哪家好
  • wordpress制作404鼓楼网页seo搜索引擎优化
  • 织梦网站 防黑seog
  • 做暖暖XO网站上海优化seo公司
  • vipsystem for wordpress苏州网站优化排名推广
  • 郑州移动网站建设株洲疫情最新情况
  • 上传网站到百度怎么样优化网站seo