我有一个ScheduledThreadPoolExecutor,我用它来安排一个以固定速率运行的任务.我希望任务以指定的延迟运行最多10次,直到它"成功".在那之后,我不希望重试任务.所以基本上我需要停止运行计划任务,当我希望它被停止时,但不关闭ScheduledThreadPoolExecutor.知道我怎么做吗?
这是一些伪代码 -
public class ScheduledThreadPoolExecutorTest
{
public static ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(15); // no multiple instances, just one to serve all requests
class MyTask implements Runnable
{
private int MAX_ATTEMPTS = 10;
public void run()
{
if(++attempt <= MAX_ATTEMPTS)
{
doX();
if(doXSucceeded)
{
//stop retrying the task anymore
}
}
else
{
//couldn't succeed in MAX attempts, don't bother retrying anymore!
}
}
}
public void main(String[] args)
{
executor.scheduleAtFixedRate(new ScheduledThreadPoolExecutorTest().new MyTask(), 0, 5, TimeUnit.SECONDS);
}
}
Run Code Online (Sandbox Code Playgroud) 这是我的代码的样子,目前还不清楚如何/为什么executorService.submit(work::get)会ClassNotFoundException在有问题的匿名类上抛出 a 。它不会一直发生,但是一旦遇到此异常,它似乎无法恢复——随后的请求会遇到相同的异常。任何人都知道可能导致这种情况发生的原因是什么?
编辑:我可以确认对这个方法的所有调用在 VM 会话中都有效,或者没有一个有效 - 它不像某些成功而其他人由于上述异常而失败。
进一步编辑:https : //bugs.openjdk.java.net/browse/JDK-8148560正是我遇到的错误,但由于不可重现和/或记者没有回应,该错误已关闭。不知何故,由 lambda 表达式产生的匿名类型看起来像是在执行程序执行表达式之前被垃圾收集,但显然并非总是如此。使用的 jdk 是openjdk1.8.0_221.
package com.ab.cde.ct.service.impl;
@Service
public class IngestionService {
@Autowired private TransactionTemplate transactionTemplate;
@Autowired private AsyncTaskExecutor executorService;
@Transactional
public void ingest(Data data) {
Supplier<Optional<String>> work = () -> transactionTemplate.execute(s -> {
// actual work on the data object, enclosed in a try/catch/finally
});
executorService.submit(work::get); // this is where the exception gets thrown
}
}
Run Code Online (Sandbox Code Playgroud)
这是异常堆栈跟踪的样子(行号不对应,因为上面的代码只是一个原型):
2019-10-23 19:11:35,267|[http-apr-26001-exec-10]|[B6AC864143092042BBB4A0876BB51EB6.1]|[]|[ERROR] web.error.ErrorServlet [line:142] …Run Code Online (Sandbox Code Playgroud) 我已经为特定的应用需求创建了一个MOJO.我已经遵循maven的指导命名插件,所以我没有提到完整
mvn groupId:artifactId:version:goal
Run Code Online (Sandbox Code Playgroud)
用于执行我的插件(我将其命名为匹配格式$ {prefix} -maven-plugin).我甚至在插件POM的配置部分中包含了'goalPrefix'属性.以下是我对插件的POM所做的嗅探:
<configuration>
<goalPrefix>${prefix}</goalPrefix>
</configuration>
Run Code Online (Sandbox Code Playgroud)
但我仍然无法执行我的插件,mvn ${prefix}:goal因为它抱怨它无法在任何存储库中找到插件.我仍然不得不使用mvn groupId:artifactId:version:goal任何想法为什么?
我commons-configuration2在我的一个项目中使用Apache .最近,我决定让Maven管理我的项目.哪个工作正常,除了commons-configuration2; 我无法找到对Maven回购的依赖.即使只是寻找commons-configuration,而不是commons-configuration2,我得到的结果都没有org.apache.commons.
我究竟做错了什么?
我有这段代码,在输入元素的值发生变化时,我监听并更新一些其他字段。
问题是,当我通过输入某些内容并使用commandButtonid:来更改此输入字段的值时submit,它会触发侦听器(可能是因为在字段上检查值的更改blur),但不会触发侦听器X,即的actionListener为commandButton. 因此,我必须再次单击该按钮才能X调用侦听器。
当然,如果我通过单击其他地方然后单击 来导致输入字段模糊,它会按预期工作commandButton。看起来,当输入字段的模糊与单击 重合时commandButton,只会发生模糊 - 按钮的单击将被忽略。有任何想法吗?
<p:inputText ...>
<p:ajax event="change" update="..." listener="..." />
</p:inputText>
...
<p:commandButton id="submit" ... actionListener="X" />
Run Code Online (Sandbox Code Playgroud)
编辑:我使用 PrimeFaces v5.1 和 JSF v2.1.19,FWIW。
该文档说明的是,该LTW具有通过使用来启动或者 <context:load-time-weaver/>XML指令或使用的@EnableLoadTimeWeaving注释.但是,我没有做过,但我仍然看到在我的项目中正确编织了各个方面!
我无法maven-fluido-skin完全完成工作 - 左侧导航部分(包含各种项目详细信息的链接的部分)始终呈现为空。不确定我是否缺少某些皮肤配置,但我认为该功能可以开箱即用地供用户使用。

我添加的任何其他小工具(例如 )google search确实会显示在左侧导航部分中。
这是我的样子site descriptor:
<project xmlns="http://maven.apache.org/DECORATION/1.4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n xsi:schemaLocation="http://maven.apache.org/DECORATION/1.4.0 http://maven.apache.org/xsd/decoration-1.4.0.xsd"\n name="Project name">\n <skin>\n <groupId>org.apache.maven.skins</groupId>\n <artifactId>maven-fluido-skin</artifactId>\n <version>1.3.1</version>\n </skin>\n\n <custom>\n <fluidoSkin>\n <profile>pre-release</profile>\n <sourceLineNumbersEnabled>true</sourceLineNumbersEnabled>\n <breadcrumbDivider>\xc2\xbb</breadcrumbDivider>\n </fluidoSkin>\n </custom>\n</project>\nRun Code Online (Sandbox Code Playgroud)\n\n我使用maven发行版的3.4版本maven-site-plugin和版本。1.3.1maven-fluido-skin2.2.1
FWIW,默认皮肤工作得很好 - 它的左侧导航正确。
\n我能够根据我的基本需求获得@annotation切入点.
@Pointcut ("@annotation(path.to.my.CustomAnnotation)")
public void actionAnnotatedPointCut() {}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试将它绑定到下面的建议体时,我得到了IllegalArgumentException.
@Pointcut ("@annotation(customAnnotation)")
public void actionAnnotatedPointCut(CustomAnnotation customAnnotation) {}
Run Code Online (Sandbox Code Playgroud)
例外:
Caused by: java.lang.IllegalArgumentException: error at ::0 incompatible number of arguments to pointcut, expected 1 found 0
Run Code Online (Sandbox Code Playgroud)
完整跟踪:
Jan 29, 2014 9:45:29 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
Jan 29, 2014 9:45:29 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.context.annotation.internalPersistenceAnnotationProcessor': Initialization of bean failed; nested exception is …Run Code Online (Sandbox Code Playgroud) 所以我一直在使用ExpectedException来预期抛出异常的types、messages、causes 甚至matchers。
但是为什么没有一个实用方法来简单地完全期望一个异常对象呢?当然,我最好测试对象相等性(假设我知道我在做什么)而不是测试它的所有单个属性?毕竟,如果 amethod A仅仅抛出它的委托人method B抛出(method A调用method B)的任何异常,那么我模拟method B抛出异常会容易得多,比如说Exception A并检查我method A的测试方法中抛出的确切对象?
那么为什么不提供这样的实用方法呢?
当然,我可以method A在我的测试中包装我的调用并捕获异常来测试对象,但同样可以说types、messages 和causes - 我想目标是使测试代码看起来更具可读性它没有 try/catch 块。
java ×7
maven ×3
aspectj ×2
spring ×2
spring-aop ×2
ajax ×1
jsf ×1
junit ×1
lambda ×1
maven-plugin ×1
pointcut ×1
primefaces ×1
scheduler ×1
scheduling ×1
unit-testing ×1