在 Spring Boot 應用程序中實現工作進程 (Implementing worker processes in a Spring Boot application)


問題描述

在 Spring Boot 應用程序中實現工作進程 (Implementing worker processes in a Spring Boot application)

簡介

我目前正在單個網絡測功機上通過 Heroku 運行 Spring‑Boot 應用程序。由於大量密集的後台任務(從 3rd 方 API 獲取資源、發送郵件等),我想將所有這些“繁重的工作”轉移到 在第二個工人測功機/進程上。但是,在將應用程序組件(例如@Repositories)正確地暴露給第二個工作進程時,我面臨著一些困難。

到目前為止我所做的嘗試

我創建了第二個主類 (BackgroundWorker),我在 Procfile 中將其指定為工作進程。然後調用以下類來初始化後台任務。

<code>@Service
@EnableMongoRepositories("com.a.viz.db")
@ComponentScan("com.a.viz.db")
@EntityScan("com.a.viz.model")
public class TaskHandler {
@Autowired
UProductRepository productRepository;

public void initScheduler()
{
    Runnable fetchProducts = () ‑&gt; {
        Scheduler.fetchProducts(productRepository);
    };
}

}
</code></pre>

雖然主類看起來像這樣:

public class BackgroundWorker {
static Logger logger = LoggerFactory.getLogger(BackgroundWorker.class);

public static void main(String[] args) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.scan(&quot;com.a.viz.workers&quot;);
    context.refresh();
    TaskHandler handler = context.getBean(TaskHandler.class);
    handler.initScheduler();
}

}
</code></pre>

在運行上面的代碼片段時,我在 UProductRepository 的具體實現中註入了 bean MongoTemplate 的不滿意依賴錯誤,稱為 UProductRepositoryImpl

public class UProductRepositoryImpl implements UProductRepositoryCustom {
private final MongoTemplate mongoTemplate;

@Autowired
public UProductRepositoryImpl(MongoTemplate mongoTemplate) {
    this.mongoTemplate = mongoTemplate;
}

}
</code></pre>

org.springframework.beans.factory.NoSuchBeanDefinitionException:沒有 'org.springframework.data.mongodb 類型的合格 bean。 core.MongoTemplate'

如何將 MongoTemplate 暴露給第二個工作進程?此外,什麼是處理這種事情的好方法?我應該嘗試組織我的組件,以便只有相關的組件暴露給工作進程嗎?感謝您的關注!


參考解法

方法 1:

Solution

Since the worker process must also be a Spring application (in order to allow for injecting repositories and such), its application context must be initialized as such. The web parameter is to prevent a proper web server being set up, since that is not necessary.

// Other configs..
@EnableAutoConfiguration
public class BackgroundWorker implements ApplicationRunner {

    @Autowired
    // Repositories..

    public static void main(String[] args)
    {
        new SpringApplicationBuilder(BackgroundWorker.class)
                .web(WebApplicationType.NONE)
                .run(args);
    }

(by KanghuKanghu)

參考文件

  1. Implementing worker processes in a Spring Boot application (CC BY‑SA 2.5/3.0/4.0)

#spring-data-mongodb #spring-boot #spring #Heroku #worker






相關問題

如何在 mongodb 中保存 java.sql.date 對象? (How to save java.sql.date object in mongodb?)

Spring Data MongoDB Core 1.9.1.RELEASE 給出 java.lang.NoClassDefFoundError: org/springframework/data/geo/GeoResults (Spring Data MongoDB Core 1.9.1.RELEASE gives java.lang.NoClassDefFoundError: org/springframework/data/geo/GeoResults)

我可以結合 AND 和 OR 條件嗎 (Can I combine AND and OR conditions)

查詢數據時應該使用 MongoTemplate 還是 DBCollection (Should i use MongoTemplate or DBCollection when query data)

是否有可能使用 Spring Data MongoDb 來定義自定義標準? (Is possibility using Spring Data MongoDb to define custom Criteria?)

Spring-data 2.1 使用 kotlin 獲取“UnsupportedOperationException:沒有訪問器設置屬性” (Spring-data 2.1 get "UnsupportedOperationException: No accessor to set property" with kotlin)

調用頁面大小大於 36 的 Spring Data MongoDB 存儲庫方法時出現 StackOverflowError (StackOverflowError when calling Spring Data MongoDB repository method with a page size bigger than 36)

JSON- MongoDB 中帶有 Spring Data 的數組 (JSON- Array in MongoDB with Spring Data)

Spring Data Mongo DB:回复消息長度5502322小於最大消息長度 (Spring Data Mongo DB: The reply message length 5502322 is less than the maximum message length)

Spring Boot 反應式和 mongodb '命令插入需要身份驗證' (Spring boot reactive and mongodb 'command insert requires authentication')

使用 MongoDB Spring Data Aggregation 匹配日期時間字段的問題 (Problem with matching date-time fields using MongoDB Spring Data Aggregation)

在 Spring Boot 應用程序中實現工作進程 (Implementing worker processes in a Spring Boot application)







留言討論