双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

SSH 非web请求是获取不到request对象,加监听器还是不行,求各路大神救救我

Old-Summer Old-Summer 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区


目前在做微信公众号,需要从系统开启后每隔两小时从腾讯服务器获取一次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)



...全文
评论 13
收藏 已收藏
分享
返回顶部
Old-Summer
Web 开发 81,108 成员 341,721 内容