Java Spring服务错误处理
我实现了Spring TaskExecutor(相当于JDK1.5的Executor)来处理从外部系统接收的通知 仅与一种方法进行接口:Java Spring服务错误处理,java,spring,multithreading,exception,executorservice,Java,Spring,Multithreading,Exception,Executorservice,我实现了Spring TaskExecutor(相当于JDK1.5的Executor)来处理从外部系统接收的通知 仅与一种方法进行接口: public interface AsynchronousService { void executeAsynchronously(Runnable task); } 以及相应的实施: public class AsynchronousServiceImpl implements AsynchronousService { private
public interface AsynchronousService {
void executeAsynchronously(Runnable task);
}
以及相应的实施:
public class AsynchronousServiceImpl implements AsynchronousService {
private TaskExecutor taskExecutor;
@Override
public void executeAsynchronously(Runnable task) {
taskExecutor.execute(task);
}
@Required
public void setTaskExecutor(TaskExecutor taskExecutor) {
this.taskExecutor = taskExecutor;
}
}
任务执行器(遗留应用程序)的Xml配置:
现在,如果出了问题,或者你怎么知道出了问题呢?例如,如果其中一个任务引发异常?你是怎么处理的?如果抛出异常,我想记录日志
我发现一篇文章()建议重写类的ThreadPoolExecutor
方法afterExecute()。但是,我目前正在使用Spring的ThreadPoolTaskExecutor
,它没有Java的ThreadPoolExecutor
的beforeExecute()
和afterExecute()
回调方法
我可以扩展ThreadPoolTaskExecutor
并重写initializeExecutor()
方法,并创建自定义ThreadPoolExecutor
的实例。但问题是initializeExecutor
方法使用ThreadPoolTaskExecutor
的私有字段
是否有人有更好的想法或方法
或者,您可以直接使用构造函数注入设置ThreadPoolExecutor实例,或者使用指向Executors类的工厂方法定义。要将这种原始执行器公开为Spring TaskExecutor,只需使用ConcurrentTaskExecutor适配器将其封装
但是我没有看到任何与ThreadPoolExecutor
相关的构造函数,我们可以在其中注入,所以这可能是一个神话,或者他们已经删除了该功能
幸运的是,我们有ThreadPoolExecutoryFactoryBean
来拯救:
如果您更喜欢本机ExtutoService的曝光,请考虑TycPoPielExtRePraceFiffyBeA作为该类的替代品。
此执行器公开了一个我们可以自定义线程池的方法:
public class NotificationPool extends ThreadPoolExecutorFactoryBean {
@Override
protected BlockingQueue<Runnable> createQueue(int queueCapacity) {
return new PriorityBlockingQueue<>(queueCapacity);
}
@Override
protected ThreadPoolExecutor createExecutor(int corePoolSize, int maxPoolSize, int keepAliveSeconds, BlockingQueue<Runnable> queue, ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) {
return new YourCustomThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveSeconds, TimeUnit.SECONDS, queue, threadFactory, rejectedExecutionHandler);
}
}
谢谢你的回答。谢谢你这么晚才申请。但问题是无法将ThreadPoolExecutor转换为TaskExecutor无法将类型为“org.example.yourcusthreadpoolexecutor”的属性值转换为所需类型“org.example.task.TaskExecutor”
我想我需要用TaskExecutor适配器
而不是TaskExecutor
将其连接起来,但是,我没有设法让它工作,因此根据您的需要更改返回Executor(或ExecutorService)的方法,但我需要使用ThreadPoolExecutor
,或者您的确切意思是什么?对当前代码进行3次更改:1。创建CustomThreadPoolExecutor以处理异常。2.更改NotificationPool以接受CustomThreadPoolExecutor。3.将您的执行人指定为执行人,而不是TaskExecutor
public class NotificationPool extends ThreadPoolTaskExecutor {
@Override
protected BlockingQueue<Runnable> createQueue(int queueCapacity) {
return new PriorityBlockingQueue<>(queueCapacity);
}
}
public class NotificationTask implements Runnable, Comparable<NotificationTask> {
private final NotificationService notificationService;
private final Notification notification;
public NotificationService(NotificationService notificationService,
Notification notification) {
this.notificationService = notificationService;
this.notification = notification;
}
@Override
public int compareTo(NotificationTask task) {
return notification.getTimestamp().compareTo(task.getTimestamp());
}
@Override
public void run() {
notificationService.processNotification(notification);
}
}
asynchronousService.executeAsynchronously(new NotificationTask (notificationService, notification));
public class NotificationPool extends ThreadPoolExecutorFactoryBean {
@Override
protected BlockingQueue<Runnable> createQueue(int queueCapacity) {
return new PriorityBlockingQueue<>(queueCapacity);
}
@Override
protected ThreadPoolExecutor createExecutor(int corePoolSize, int maxPoolSize, int keepAliveSeconds, BlockingQueue<Runnable> queue, ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) {
return new YourCustomThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveSeconds, TimeUnit.SECONDS, queue, threadFactory, rejectedExecutionHandler);
}
}
public class YourCustomThreadPoolExecutor extends ThreadPoolExecutor {
public YourCustomThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
}
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
// Here do something with your exception
}
}