Java Spring TaskExecutor实现队列优先级

Java Spring TaskExecutor实现队列优先级,java,spring,queue,executor,Java,Spring,Queue,Executor,我正在处理的应用程序接收来自外部系统的通知,我希望处理这些通知 到目前为止,我有以下实现: public class AsynchronousServiceImpl implements AsynchronousService { private TaskExecutor taskExecutor; @Override public void executeAsynchronously(Runnable task) { taskExecutor

我正在处理的应用程序接收来自外部系统的通知,我希望处理这些通知

到目前为止,我有以下实现:

    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;
    }
}
spring配置(我只需要1个线程,因为我不想并行执行通知,因为有些遗留问题很难更改)

我拥有的这个通知对象包含一个时间戳字段。 我想按此字段对队列中的通知进行优先级排序(我认为Spring默认使用无界队列,这对我来说更合适,因为我需要一个无界队列)


我可以在spring应用程序中以某种方式集成它,而不必从头开始手动实现它吗?因此,我希望根据通知对象上的时间戳字段对队列中的TaskS(可运行对象)进行排序。(正是我传递给“processNotification”方法的对象)

ThreadPoolTaskExecutor
BlockingQueue
支持:

protected BlockingQueue<Runnable> createQueue(int queueCapacity) {
    if (queueCapacity > 0) {
        return new LinkedBlockingQueue<Runnable>(queueCapacity);
    }
    else {
        return new SynchronousQueue<Runnable>();
    }
}

谢谢你的回答。不过我有一个问题。最后,我需要使用我的新池(YourPool)。但是我需要重写bean(taskExecutor)的方法(createQueue),以便重用它?是的。您必须重写方法createQueue才能像我的示例代码一样使用自定义队列。你的bean必须是“yourPool”类型是的,我这样做了-我只是想知道在我的xml文件中定义的taskExecutor-bean是否过时了?如果你仍然使用xml来配置你的bean,那么你必须在
class=
Attribute中提供
yourPool
属性,并且在类中:“YourTask”我称该方法为“someMethod”它在另一个服务中定义。但是我不能自动连接(YourTask类中的此服务)Spring中使用new创建的对象。我所关心的全部问题是,谁应该将其集成到SpringMVC应用程序中
 asynchronousService.executeAsynchronously(new Runnable() {
     @Override
     public void run() {
         someMethod.processNotification(notification)
      }
   });
protected BlockingQueue<Runnable> createQueue(int queueCapacity) {
    if (queueCapacity > 0) {
        return new LinkedBlockingQueue<Runnable>(queueCapacity);
    }
    else {
        return new SynchronousQueue<Runnable>();
    }
}
public class YourPool extends ThreadPoolTaskExecutor {
    @Override
    protected BlockingQueue<Runnable> createQueue(int queueCapacity) {
        return new PriorityBlockingQueue<>(queueCapacity);
    }
}
public class YourTask implements Runnable, Comparable<YourTask> {
    private Notification notification;

    public YourTask(Notification notification) {
        this.notification = notification;
    }
    @Override
    public void run() {
        someMethod.processNotification(notification)
    }

    @Override
    public int compareTo(B other) {
        // Here you implement the priority
        return notification.getTimestamp().compareTo(other.notification.getTimestamp());
    }
}
asynchronousService.executeAsynchronously(new YourTask(notificationX));