为了账号安全,请及时绑定邮箱和手机立即绑定

执行新的可运行文件时丢失 ApplicationContext

执行新的可运行文件时丢失 ApplicationContext

潇湘沐 2021-09-12 17:27:12
我知道我对这个春天的东西很陌生,但我一整天都被困在这个问题上。我不太喜欢提问,但也许我会得到一个想法。所以这是我的问题:我正在尝试创建一个队列来处理后端的内容。我通过在组件类中创建一个静态 executorservice 并使用帮助方法来运行它们来做到这一点。它似乎像我想要的那样工作,当我在类中连接时,我可以进入这些类,但是当它们运行时,它们似乎丢失了应用程序上下文(或者这只是我的猜测)。我确信有更好的方法可以做到这一点,但在我正在使用的自定义框架中,有许多功能对我不起作用。我没有 spring-config.xml,不能使用@Configuration执行器服务组件@Componentpublic 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 环境中运行良好@Componentpublic class OrderAllocationTestJob implements Runnable {int i;public OrderAllocationTestJob(int i) {    this.i = i;}@Overridepublic 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);    }这是我最终得到的例外情况“范围'请求'对于当前线程无效”
查看完整描述

3 回答

?
一只甜甜圈

TA贡献1836条经验 获得超5个赞

我通过为我的可运行对象扩展 ConcurrentLinkedQueue 并将它们保存在我在 ServletContextListener 的 initialize 方法中实例化的管理器中解决了这个解决方案。通过覆盖 ConcurrentLinkedQueue 的 offer() 方法来持续轮询直到队列为空,我能够同步处理可运行对象。

不幸的是,这会锁定请求线程,直到 runnable 完成,我将不得不让我的用户密切关注它并让我知道页面是否最终运行很长时间,但至少在我的测试环境中,该过程似乎是亚秒即使我一次打 20 个,所以我现在还好。

我仍然更喜欢从我的 Tomcat 容器执行的 ExecutorService 但在请求范围之外,但除非有人可以回答这个问题,否则我现在只好离开它



查看完整回答
反对 回复 2021-09-12
?
吃鸡游戏

TA贡献1829条经验 获得超7个赞

“我相信有更好的方法可以做到这一点”

基于此,您需要在调用另一个线程之前创建/查找所有请求和会话范围的组件。实际上,请求注入是线程本地的,无法在您的场景中工作。


查看完整回答
反对 回复 2021-09-12
?
宝慕林4294392

TA贡献2021条经验 获得超8个赞

你看起来像那样吗?


@Component 公共类 AsynchronousThread 扩展了 Thread {


public static final Logger LOGGER = LoggerFactory

        .getLogger(AsynchronousThread.class);


@Autowired

private Writer writer;



private BlockingQueue<IndexContextDTO> blockingQueue = new LinkedBlockingQueue<IndexContextDTO>(

        500);


/**

 * 

 */

public AsynchronousThread() {

    super("AsynchronousThread");

}



@PostConstruct

public void init() {

    Integer internalQueueSize = 100;

    this.blockingQueue = new LinkedBlockingQueue<>(internalQueueSize);

    this.start();

}



@Override

public void run() {


    while (true) {

        // Do stuff

    }

}



public void putInQueue(IndexContextDTO message) {

    try {

        this.blockingQueue.put(message);

    } catch (InterruptedException interruptedException) {

        // This exception will be thrown in very rare case.

        LOGGER.error("An error while putting message in the queue. "

                + message, interruptedException);

    }

}

}


查看完整回答
反对 回复 2021-09-12
  • 3 回答
  • 0 关注
  • 218 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信