- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Spring TaskExecutor 使用不同的线程运行一个方法。我有一个 Autowiring 的依赖项,我得到一个 NullPointerException 。 Autowiring 的依赖项为空。请帮忙。
TaskExecutor 类如下:
public class WebClientTaskExecutor {
@Autowired
WebClientService webClientService;
public class SyncMails implements Runnable {
private String userName;
private Store store;
public SyncMails(String userName, Store store) {
this.userName = userName;
this.store = store;
}
@Override
public void run() {
try {
System.out.println("inside sync mails run method");
String[] folderNames = { "inbox", "sent", "trash", "drafts" };
for (String folderName : folderNames) {
//Null pointer exception in below line.webClientservice=null
int messageCount = WebClientTaskExecutor.this.webClientService.getMessageCount(
folderName, this.store);
int dbLatestMessageNumber = WebClientTaskExecutor.this.webClientService
.getLatestMessageNumberFromDb(folderName,
this.userName, 1);
System.out.println("MEssage count---->" + messageCount);
System.out.println("Latest message count dao--->"
+ dbLatestMessageNumber);
if (messageCount > dbLatestMessageNumber) {
WebClientTaskExecutor.this.webClientService.getMailsFromImap(folderName,
messageCount, dbLatestMessageNumber + 1,
this.store, this.userName);
}
}
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getUserName() {
return userName;
}
public void setUser(String userName) {
this.userName = userName;
}
public Store getStore() {
return store;
}
public void setStore(Store store) {
this.store = store;
}
}
private TaskExecutor taskExecutor;
public WebClientTaskExecutor(TaskExecutor taskExecutor) {
this.taskExecutor = taskExecutor;
}
public void syncMails(String userName,Store store){
System.out.println("web client service object---->" + webClientService);
this.taskExecutor.execute(new SyncMails(userName, store));
}
}
在 Controller 中,我正在创建 thrTaskExecutor 对象并将其提供给 WebClientTaskExecutor 构造函数。并启动 syncMails 方法。
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(20);
executor.setQueueCapacity(50);
executor.initialize();
WebClientTaskExecutor taskExecutor = new WebClientTaskExecutor(executor);
taskExecutor.syncMails(userName, store);
最佳答案
@Autowired
注解只能在 Spring bean 类中工作。因此,您必须将您的类声明为 Spring bean,或者通过正确注释它(@Component、@Service、@Repository、@Controller
)或者通过在 Spring XML 上下文文件中定义这个 bean。
您也不能自己创建 Spring bean 的实例,而是使用 @Autowired
注释。
关于java - spring TaskExecutor 类中的 Autowiring 对象为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29726819/
我正在考虑使用 Java 的 TaskExecutor 来触发异步数据库写入。线程不是免费提供的,这是可以理解的,但假设我使用的是 5-10 大小的固定线程池,这怎么可能是个坏主意? 我们的应用程序使
我正在开发的应用程序接收来自外部系统的通知,我希望按顺序处理这些通知,因为我遇到了一些死锁。 我使用 Spring 的 TaskExecutor,它相当于 JDK 1.5 的 Executor。 我通
我有一个 REST 网络服务,想记录所有传入和传出的 XML 请求。由于它们可能非常大,而且我还必须应用某种转换,所以我想在异步线程中执行它。 到目前为止,我只是在记录器方法上使用了 @Async 注
在 Spring 中有没有一种方法(使用 TaskExecutor )并行执行多个任务,然后等待(障碍)直到所有任务完成? 我在这里找到了一个例子http://www.mkyong.com/sprin
让我们考虑一下我有以下几点: public class MyRunnable implements Runnable { public void run() { //do so
我已经使用了 Spring 和 futures 中的 TaskExecutor 从任务中获取 future ,但是现在,当某些任务在一段时间内没有响应时,我必须停止这些任务。我尝试过使用 Future
遵循我使用 Spring 3 TaskExecutor 进行异步邮件发送的代码。代码工作正常,我只是想知道是否有任何邮件因任何原因未能发送,那么必须将其再次添加到队列中并重试发送。 我已在 Googl
我有一个 Web 服务 DocGenerationServiceImpl,它使用 DocRepository 和将记录表示为 DocFileDO 的对象在表中插入(对于每种格式)记录。在 for 循环
我在 DefaultMessageListenerContainer 内使用 SimpleAsyncTaskExecutor,并且我想使用 JMX mbean 监视 Activity 线程计数。我创建
我正在编写一个同时(并行)执行多个线程的程序,我正在使用 TaskExecutor 。 @Autowired TaskExecutor threadPoolTaskExecutor; @Test pu
我是 Apache Flink 的新手,所以我目前正在尝试做一些实验。我正在阅读 Kafka 的主题,然后在控制台上打印出来。在打印大约 100k+ kafka 消息后,它抛出异常。日志输出如下。 我
我如何使用 spring TaskExecutor 来生成任务,这样就不会抛出内存不足异常。 当前任务池配置: 并且我通过使用加载 bean 在我的请求处理程序中使用
我希望 spring 从 TaskExecutionAutoConfiguration 加载默认的 ThreadPoolTaskExecutor。虽然我想为一些明确的副任务提供可能拥有的额外执行程
我已经用任务执行器设置了文件轮询器 ExecutorService executorService = Executors.newFixedThreadPool(10); LO
你怎么会有一个实现Runnable并自动提交给springsTaskExecutor的类? 例如,我有一个任务: public class MyTask implements Runnable {
我在 Spring Boot 应用程序中做了一个 @Service 类,其中一种方法应该异步运行。当我读到方法应该是 @Async 注释并且我必须运行一个 TaskExecutor bean。但在 S
设置:Apache Tomcat 8 Web服务器/Java Spring框架/MVC 背景:我有多台服务器连接了一个负载均衡器。每次负载平衡器将请求转发到Web服务器时,服务器会将请求推送到远程队列
我可以在 Spring Batch 中将 FlatfileItemReader 与 Taskexecutor 一起使用吗? 我已经使用 ThreadPoolTaskExecutor 实现了 Fla
我有这样的 java spring boot 配置: @Bean public SubscribableChannel channel(MessageHandler handler) { Pub
我正在寻找一种方法,让 spring bean 将自己注册到作业处理器 bean,作业处理器 bean 将按计划执行已注册的 bean。 我希望 bean 只需要实现一个接口(interface),并
我是一名优秀的程序员,十分优秀!