package com.yanzuoguang.cloud.aop.log; import com.yanzuoguang.cloud.CloudConfig; import com.yanzuoguang.cloud.aop.LogFeign; import com.yanzuoguang.util.thread.ThreadNext; import com.yanzuoguang.util.vo.LogVo; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanInitializationException; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.*; import org.springframework.stereotype.Component; import java.util.concurrent.LinkedBlockingQueue; /** * 获取当前日志对象 * * @author 颜佐光 */ @Component public class LogBase implements ThreadNext.Next, InitializingBean, ApplicationContextAware { /** * 缓存队列 */ protected volatile LinkedBlockingQueue<LogVo> cache = new LinkedBlockingQueue<>(); @Autowired private CloudConfig cloudConfig; private LogFeignBase logFeign; /** * Set the ApplicationContext that this object runs in. * Normally this call will be used to initialize the object. * <p>Invoked after population of normal bean properties but before an init callback such * as {@link InitializingBean#afterPropertiesSet()} * or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader}, * {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and * {@link MessageSourceAware}, if applicable. * * @param applicationContext the ApplicationContext object to be used by this object * @throws ApplicationContextException in case of context initialization errors * @throws BeansException if thrown by application context methods * @see BeanInitializationException */ @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { // 获取个人实例话的对象 try { this.logFeign = applicationContext.getBean(LogFeign.class); } catch (Exception ex) { System.err.println("请添加处理日志服务,实现LogFeign接口!"); } // 默认实例话对象 if (this.logFeign == null) { this.logFeign = applicationContext.getBean(LogFeignBase.class); } } /** * Invoked by a BeanFactory after it has set all bean properties supplied * (and satisfied BeanFactoryAware and ApplicationContextAware). * <p>This method allows the bean instance to perform initialization only * possible when all bean properties have been set and to throw an * exception in the event of misconfiguration. * * @throws Exception in the event of misconfiguration (such * as failure to set an essential property) or if initialization fails. */ @Override public void afterPropertiesSet() throws Exception { // 判断是否要记录日志 if (cloudConfig.isLogBase()) { ThreadNext.start(this, "save log error"); } } /** * 添加日志到缓存中,并不是立即添加,而是通过线程执行,防止对环境造成影响 * * @param logVo */ public void addLog(LogVo logVo) { // 当不记录日志时,则直接忽略 if (!cloudConfig.isLogBase()) { return; } cache.add(logVo); } /** * 执行下一个函数,出现异常会继续执行 * * @return 是否继续执行 * @throws Exception 异常信息 */ @Override public boolean next() { if (cache.isEmpty()) { return true; } // 循环处理日志信息 while (cache.size() > 0) { // 取出一条日志 LogVo item = cache.poll(); // 判断日志信息是否为空 if (item == null) { continue; } // 保存日志 try { logFeign.save(item); } catch (Exception ex) { ex.printStackTrace(); } } return true; } /** * 沉睡时间 * * @return */ @Override public int getNextTime() { return 100; } }