0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Spring BootでQuartzを利用する(2)

Last updated at Posted at 2022-10-14

メインとQuartzでデータベースを分ける

■完成イメージ

+----------+            +-------------+            +------------+
|          |            | Spring Boot |   JDBC     |   MainDB   |
| Browser |<---------->|     +       |<---------->|            |
|          | port=8080  |   Quartz    | port=33060 +------------+
|          |            |             |            +------------+
|          |            |             |   JDBC     |  QuartzDB  |
|          |<---------->|             |<---------->|            |
+----------+            +-------------+ port=33060 +------------+

■構築環境

・Windows10 バージョン21H2
・IntelliJ IDEA 2022.1.4(Community Edition)
・OpenJDK Runtime Environment Zulu17.36+17-CA (build 17.0.4.1+1-LTS)
・Spring Boot 2.7.4
・Quartz
・Mysql 5.7.39

1. プロパティファイルを編集する

メインとQuartzで接頭辞を変えてDB接続部分を記述する。
今回は。メインをspring.datasource、Quartzをspring.quartz.datasourceとした。

application.properties
spring.datasource.platform=org.hibernate.dialect.MySQL5Dialect
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://***.***.***.***:33060/メインDB
spring.datasource.username=メインユーザID
spring.datasource.password=メインパスワード
    
spring.quartz.datasource.platform=org.hibernate.dialect.MySQL5Dialect
spring.quartz.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.quartz.datasource.url=jdbc:mysql://***.***.***.***:33060/quartzDB
spring.quartz.datasource.username=quartzユーザID
spring.quartz.datasource.password=quartzパスワード

2. コンフィグクラスを作成

メインとQuartzそれぞれで、DataSource Beanを分けて定義する。
@ConfigurationPropertiesアノテーションでそれぞれの接頭辞を記載し
Quartz側は、@QuartzDataSourceアノテーションを付与する。

ApplicationConfiguration.java
@Configuration
public class ApplicationConfiguration {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource mainDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @QuartzDataSource
    @ConfigurationProperties(prefix = "spring.quartz.datasource")
    public DataSource quartzDataSource() {
        return DataSourceBuilder.create().build();
    }
}

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?