SSH 非web请求是获取不到request对象,加监听器还是不行,求各路大神救救我
目前在做微信公众号,需要从系统开启后每隔两小时从腾讯服务器获取一次access_token。所以我就想着在spring容器加载完成后,调用接口请求access_token,然后把它存到request的application里区,方便全局获取统一的token。
但是尝试了好多次,都显示request对象获取不到,说是因为不是web请求没有request对象。之后我根据提示信息,加了一个RequestContextListener监听器。但是还是不起作用。
各路大神一起有没有碰到过这个问题,求指点一下。刚开始整ssh,很多地方不是太明白。犯了什么菜鸟错误还望指出。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" metadata-complete="true" version="3.1">
<display-name>rzf</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<listener>
<listener-class>com.zhejianglehua.rzf.action.util.GetAccessTokenAction</listener-class>
</listener>
<listener>
<listener-class>com.zhejianglehua.rzf.action.util.AAA</listener-class>
</listener>
<filter>
<filter-name>EcodingFilter</filter-name>
<filter-class>com.zhejianglehua.rzf.filter.EcodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>EcodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/applicationContext.xml</param-value>
</context-param>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>调用接口的监听器
package com.zhejianglehua.rzf.action.util;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpServletRequest;
import org.apache.http.ParseException;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.components.If;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.zhejianglehua.rzf.entity.weixin.po.AccessToken;
import com.zhejianglehua.rzf.service.util.WeixinUtilService;
@Namespace("/weixin")
@Controller
public class GetAccessTokenAction implements ApplicationListener<ContextRefreshedEvent> {
private HttpServletRequest request;
private Timer timer =null;
@Autowired
private WeixinUtilService weixinUtilService;
@Autowired
public void setRequest(HttpServletRequest request) {
this.request = request;
}
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
if(event.getApplicationContext().getParent() == null){
System.out.println("启动定时获取Access Token任务...");
weixinUtilService.fuck();
timer = new Timer(true);
timer.schedule(new TimerTask() {
@Override
public void run() {
try {
AccessToken token = weixinUtilService.getAccessToken();
request.getServletContext().setAttribute("token", token);
} catch (ParseException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, 10000, 5000);// 延迟0s后执行,每55分钟执行一次
}
}
}报错
信息: Server startup in 7467 ms
Exception in thread "Timer-0" java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)
at org.springframework.web.context.support.WebApplicationContextUtils.currentRequestAttributes(WebApplicationContextUtils.java:275)
at org.springframework.web.context.support.WebApplicationContextUtils.access$400(WebApplicationContextUtils.java:64)
at org.springframework.web.context.support.WebApplicationContextUtils$RequestObjectFactory.getObject(WebApplicationContextUtils.java:291)
at org.springframework.web.context.support.WebApplicationContextUtils$RequestObjectFactory.getObject(WebApplicationContextUtils.java:286)
at org.springframework.beans.factory.support.AutowireUtils$ObjectFactoryDelegatingInvocationHandler.invoke(AutowireUtils.java:307)
at com.sun.proxy.$Proxy14.getServletContext(Unknown Source)
at com.zhejianglehua.rzf.service.util.WeixinUtilService.getAccessToken(WeixinUtilService.java:227)
at com.zhejianglehua.rzf.action.util.GetAccessTokenAction$1.run(GetAccessTokenAction.java:64)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)
...全文
相关推荐
Web开发+java+ssh框架
Web开发+java+ssh框架,学习的可以看看
ssh框架下用ajax动态从数据库查询的菜单
ssh框架下用ajax动态从数据库查询的菜单
SSH整合的配置文件详解
详细的SSH个框架整合的配置文件,希望对大家有帮助!
j2ee SSH 整合笔记,献于新手。。
合ssh 思路,用spring 管理 struts 的action ,与hibernate 的dao 持久层。 先载入jar包,在myeclipse里 增加 struts 增加 hibernate 增加 spring。 然后去网上下载 最新的ssh jar包,覆盖调 web-info\lib\的所有jar包。 因为myeclipse 里的版本可能继承的是很老的包,所以建议在网上下最新的。 第一步,配置struts ,跟原来的配置一样,唯一不同的是。 type用到了spring的一个代理 action。 第二步 配置hibernate,我们打开hibernate.cfg.xml 设置一系列的参数。 ....
ssh集成笔记
ssh整合 struts+spring+hibernate(ssh)集成问题


Web 开发

