NutzCN Logo
问答 如何在定时调度中获取到request
发布于 2195天前 作者 码农 5588 次浏览 复制 上一个帖子 下一个帖子
标签:

我这样获取的时候会报request为null,求大神指点

@IocBean
public class LoginOutJob  implements Job {
    @Inject
    private SysLoginController loginController;
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        ActionContext actionContext = new ActionContext();
        HttpServletRequest request = actionContext.getRequest();
        HttpSession session = request.getSession();
        checkLoginOut(session);
    }
    public void checkLoginOut(HttpSession session){
        try {
            Object _token = session.getAttribute("token");
            String token = _token.toString();
            StringBuffer buffer = new StringBuffer();
            String url = "http://172.16.64.72:8090/bam/identity/json/attributes?subjectid="+token;
            URI uri = new URI(url);
            URL urls = uri.toURL();
            HttpURLConnection connection = (HttpURLConnection) urls.openConnection();
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestProperty("Charset", "utf-8");
            connection.setRequestMethod("GET");
            connection.connect();
            InputStream inputStream = connection.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            bufferedReader.close();
            inputStreamReader.close();
            // 释放资源
            inputStream.close();
            connection.disconnect();
            String str1 = buffer.toString();
            System.err.println(str1);
            JSONObject jsonObject = new JSONObject(str1);
            JSONArray attributes = jsonObject.getJSONArray("attributes");
            String s = attributes.get(0).toString();
            JSONObject jsonObject1 = new JSONObject(s);
            JSONArray values = jsonObject1.getJSONArray("values");
            Object username = values.get(0);
            if (null==username){
                loginController.logout();
            }
        } catch (URISyntaxException e) {
            e.printStackTrace();
            loginController.logout();
        } catch (IOException e) {
            e.printStackTrace();
            loginController.logout();
        }catch (Exception e){
            e.printStackTrace();
            loginController.logout();
        }
    }
}
java.lang.NullPointerException
	at cn.wizzer.app.web.commons.quartz.job.LoginOutJob.execute(LoginOutJob.java:31)
	at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
	at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
[ERROR] 2018-04-13 16:11:30,080 org.quartz.core.ErrorLogger.schedulerError(QuartzScheduler.java:2425) - Job (logoutGroup.logout threw an exception.
org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exception: java.lang.NullPointerException]
	at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
	at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
Caused by: java.lang.NullPointerException
	at cn.wizzer.app.web.commons.quartz.job.LoginOutJob.execute(LoginOutJob.java:31)
	at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
	... 1 more
10 回复

定时任务在独立线程里,哪来请求?

@wendal 我需要从session中获取到token,有什么方法吗?

没有请求,哪来session,你的思路错了-_-||

@wendal 哦,那么我当时应该将token存储到哪呢?因为token随时会变动,所以没有考虑存到数据库中,

LoginOutJob?你这是要定时清理session?

@wendal 定时检测那个url连接是否能够获取到数据,

数据库/redis/静态变量,选一种咯

@wendal 额,没有用过静态变量,兽总能指点一番吗

public static 啊。。。

@wendal 谢了,兽总,但是还有个问题,我发现有时候定时任务只执行了一次就不执行了,后台也不报错,不知道是什么原因

添加回复
请先登陆
回到顶部