购物车拦截器获取用户信息
CartInterceptor 类代码:
//购物车拦截器
public class CartInterceptor implements HandlerInterceptor{
@Autowired
private HttpClientService httpClientService;
private static final ObjectMapper MAPPER = new ObjectMapper();
//在controller方法执行前触发
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
/*
* 步骤:
* 1、从cookie中获取ticket,如果获取不到转向登录页面;如果获取到继续
* 2、利用sso系统提供业务访问接口,查询redis,如果有继续,如果没有转向登录页面
* 3、把user.json转成User。和controller共享变量,线程访问安全。ThreadLocal
*/
String ticket=CookieUtils.getCookieValue(request,"JT_TICKET");
if(StringUtils.isNullOrEmpty(ticket)){
response.sendRedirect("/user/login.html");
return false;
}else{
String url="http://sso.jt.com/user/query/"+ticket;
String resultJson=httpClientService.doGet(url);
String userJson=MAPPER.readTree(resultJson).get("data").asText();
Long userId=MAPPER.readValue(userJson, User.class).getId();
UserThreadLocal.setUserid(userId);
System.err.println("%$%$^%$^$$");
System.out.println(userId);
return true;
}
}
//在controller方法执行后触发
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
}
//在controller方法执行后,在转向页面之前
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
}
}
UserTheadLocal类代码:
//用户的ThreadLocal形成线程安全性保障
public class UserTheadLocal {
private static final ThreadLocal<String> USERID=new ThreadLocal<>();
public static Long getUserId(){
return Long.parseLong(USERID.get());
}
public static void setUserId(Long userId){
USERID.set(String.valueOf(userId));
}
}
CartController类代码:
//我的购物车
@RequestMapping("/show")
public String myCartList(Model model){
Long userId = UserTheadLocal.getUserId();
List<Cart> cartList = dubboCartRestService.myCartList(userId);
model.addAttribute("cartList", cartList);
return "cart";
}
SpringMVC拦截器配置:
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/cart/**"/>
<bean class="com.jt.web.interceptor.CartInterceptor"></bean>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/order/**"/>
<bean class="com.jt.web.interceptor.OrderInterceptor"></bean>
</mvc:interc
eptor>
</mvc:interceptors>
Quartz定时任务
Quartz整合
实现步骤:
1.在订单系统中引入整合文件 applicationContext-scheduler.xml
配置示例:
<!-- 定义任务bean -->
<bean name="paymentOrderJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<!-- 指定具体的job类 -->
<property name="jobClass" value="com.jt.order.job.PaymentOrderJob" />
<!-- 指定job的名称 -->
<property name="name" value="paymentOrder" />
<!-- 指定job的分组 -->
<property name="group" value="Order" />
<!-- 必须设置为true,如果为false,当没有活动的触发器与之关联时会在调度器中删除该任务 -->
<property name="durability" value="true"/>
<!-- 指定spring容器的key,如果不设定在job中的jobmap中是获取不到spring容器的 -->
<property name="applicationContextJobDataKey" value="applicationContext"/>
</bean>
<!-- 定义触发器 -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="paymentOrderJobDetail" />
<!-- 每一分钟执行一次 -->
<property name="cronExpression" value="0 0/1 * * * ?" />
</bean>
<!-- 定义调度器 -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger" />
</list>
</property>
</bean>
</beans>
2.在jt-order工程下,建立自定义的定时业务类
代码示意:
public class PaymentOrderJob extends QuartzJobBean{
//底层调用时就会回调这个方法
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
//定期删除两天前的未支付订单
ApplicationContext applicationContext = (ApplicationContext) context.getJobDetail().getJobDataMap().get("applicationContext");
Date date = new DateTime().minusDays(2).toDate(); //joda工具类方便日期加减,2天前
applicationContext.getBean(OrderMapper.class).paymentOrder(date);
}
}
3.完成OrderMapper类代码:
代码示意:
public interface OrderMapper{
public Order queryByOrderId(String orderId);
public void orderAdd(Order order);
public void paymentOrder(Date date);
}
4.完成OrderMapper.xml语句:
OrderMapper.xml:
<!-- 定时任务调用,解决2天内在线支付方式的未支付订单,状态改成关闭 -->
<update id="paymentOrder" parameterType="date">
UPDATE tb_order SET STATUS=6