java - 执行新的可运行文件时丢失 ApplicationContext
问题描述
我知道我对这个春天的东西很陌生,但我整天都被困在这上面。我不太喜欢问问题,但也许我会有一个想法。
所以这是我的问题:我正在尝试创建一个队列来处理后端的内容。我通过在组件类中创建一个静态 executorservice 并使用帮助方法来运行它们来做到这一点。它似乎像我想要的那样工作,当我在类中连接时,我可以进入这些类,但是当它们运行时,它们似乎失去了应用程序上下文(或者这只是我的猜测)。
我确信有更好的方法可以做到这一点,但是在我正在使用的自定义框架中,有许多功能对我不起作用。我没有spring-config.xml,不能使用@Configuration
执行器服务组件
@Component
public class FifoComponent {
public static ExecutorService executors = Executors.newSingleThreadExecutor();
private static Lock lock = new ReentrantLock(true);
public static void executeNewTestJob(int i) {
lock.lock();
OrderAllocationTestJob job = new OrderAllocationTestJob(i);
executors.execute(job);
lock.unlock();
}
}
可运行组件 - 注意 appdateutils 有一个调用组件的方法,该组件在我的典型 tomcat 环境中运行良好
@Component
public class OrderAllocationTestJob implements Runnable {
int i;
public OrderAllocationTestJob(int i) {
this.i = i;
}
@Override
public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Asynchronous task " + i);
System.out.println(AppDateUtils.getCurrentTimeStamp());
}
}
从 struts 2 动作(测试)调用我知道我可以调用 appdateutils.gettime 方法
for (int i = 0; i < 50; i++) {
FifoComponent.executeNewTestJob(i);
}
这是我最终得到的例外,因为它的价值“范围'请求'对于当前线程不活动”
Exception in thread "pool-15-thread-50" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dateTimestampDao': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is 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.
解决方案
“我确信有更好的方法来做到这一点”
基于此,您需要在调用另一个线程之前创建/查找所有请求和会话范围的组件。实际上,请求注入是线程本地的,不能在您的场景中工作。
推荐阅读
- regex - 使用 RegEx 从给定字符串中提取策略 ID
- android - pjsua2 示例应用程序传出呼叫通过 TCP 连接获得 403 禁止响应
- sql - 如何使用 SQL select 过滤具有类别的行以根据条件连接其他(多个)表?
- python - Jupyter:找不到线条魔术功能
- javascript - Javascript中使用键或循环访问对象属性的区别
- php - 如何使用 PHP 从两个不同的表中进行选择,从结果中生成变量并对它们进行数学运算
- angular - ng2-fancy-image-uploader 不能在 Angular 8 中工作
- database - 捕获导致 ORA-01438 的列:值大于此列允许的指定精度
- android - adb remount 总是需要 adb root
- c++ - 如何从 CPP 源代码行号中获取 LLVM 位代码行号?