执行新的可运行文件时丢失 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);

    }

这是我最终得到的例外情况“范围'请求'对于当前线程无效”



潇湘沐
浏览 219回答 3
3回答

一只甜甜圈

我通过为我的可运行对象扩展 ConcurrentLinkedQueue 并将它们保存在我在 ServletContextListener 的 initialize 方法中实例化的管理器中解决了这个解决方案。通过覆盖 ConcurrentLinkedQueue 的 offer() 方法来持续轮询直到队列为空,我能够同步处理可运行对象。不幸的是,这会锁定请求线程,直到 runnable 完成,我将不得不让我的用户密切关注它并让我知道页面是否最终运行很长时间,但至少在我的测试环境中,该过程似乎是亚秒即使我一次打 20 个,所以我现在还好。我仍然更喜欢从我的 Tomcat 容器执行的 ExecutorService 但在请求范围之外,但除非有人可以回答这个问题,否则我现在只好离开它

吃鸡游戏

“我相信有更好的方法可以做到这一点”基于此,您需要在调用另一个线程之前创建/查找所有请求和会话范围的组件。实际上,请求注入是线程本地的,无法在您的场景中工作。

宝慕林4294392

你看起来像那样吗?@Component 公共类 AsynchronousThread 扩展了 Thread {public static final Logger LOGGER = LoggerFactory&nbsp; &nbsp; &nbsp; &nbsp; .getLogger(AsynchronousThread.class);@Autowiredprivate Writer writer;private BlockingQueue<IndexContextDTO> blockingQueue = new LinkedBlockingQueue<IndexContextDTO>(&nbsp; &nbsp; &nbsp; &nbsp; 500);/**&nbsp;*&nbsp;&nbsp;*/public AsynchronousThread() {&nbsp; &nbsp; super("AsynchronousThread");}@PostConstructpublic void init() {&nbsp; &nbsp; Integer internalQueueSize = 100;&nbsp; &nbsp; this.blockingQueue = new LinkedBlockingQueue<>(internalQueueSize);&nbsp; &nbsp; this.start();}@Overridepublic void run() {&nbsp; &nbsp; while (true) {&nbsp; &nbsp; &nbsp; &nbsp; // Do stuff&nbsp; &nbsp; }}public void putInQueue(IndexContextDTO message) {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; this.blockingQueue.put(message);&nbsp; &nbsp; } catch (InterruptedException interruptedException) {&nbsp; &nbsp; &nbsp; &nbsp; // This exception will be thrown in very rare case.&nbsp; &nbsp; &nbsp; &nbsp; LOGGER.error("An error while putting message in the queue. "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + message, interruptedException);&nbsp; &nbsp; }}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java