Elasticsearch+Spring定時任務報錯【not be the scheduler thread. Reason: [Blocking operation]】

報錯

java.lang.AssertionError: Expected current thread [Thread[scheduler-5,5,main]] to not be the scheduler thread. Reason: [Blocking operation]
	at org.elasticsearch.threadpool.ThreadPool.assertNotScheduleThread(ThreadPool.java:787) ~[elasticsearch-5.2.2.jar:5.2.2]
	at org.elasticsearch.common.util.concurrent.BaseFuture.get(BaseFuture.java:65) ~[elasticsearch-5.2.2.jar:5.2.2]
	at org.elasticsearch.action.support.AdapterActionFuture.actionGet(AdapterActionFuture.java:68) ~[elasticsearch-5.2.2.jar:5.2.2]

原因

代碼
client.prepareExists(indexname).execute().actionGet();
org.elasticsearch.common.util.concurrent.BaseFuture中調用ThreadPool.assertNotScheduleThread(BLOCKING_OP_REASON)保證其不爲"schedule"線程(猜想:避免阻塞調度線程?)。

 <task:scheduler id="taskController" pool-size="20"/>

因此修改scheduler爲其他名字即可。

解決

重新new一個線程或者修改beanName以改變線程名前綴。

<task:scheduler id="otherName" pool-size="10"/>

ExecutorConfigurationSupport#afterPropertiesSet會保證將beanName作爲線程的前綴。

	/**
	 * Set up the ExecutorService.
	 */
	public void initialize() {
		if (logger.isInfoEnabled()) {
			logger.info("Initializing ExecutorService " + (this.beanName != null ? " '" + this.beanName + "'" : ""));
		}
		if (!this.threadNamePrefixSet && this.beanName != null) {
			setThreadNamePrefix(this.beanName + "-");
		}
		this.executor = initializeExecutor(this.threadFactory, this.rejectedExecutionHandler);
	}

爲了防止阻塞調度線程,再將代碼 actionGet/get添加了超時時間

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章