【Spring】Scheduling Tasks
Spring Webアプリで定期実行処理を実現するためには、ScheduledTasksを使う。便利。
Applicationクラスに@EnableSchedulingを追加する
@SpringBootApplication @EnableScheduling public class StsTodoApplication { ... }
コンポーネント作成
@Component @Slf4j public class ScheduledTasks { @Scheduled(fixedRate = 60000) public void reportCurrentTime() { SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); log.info("The time is now {}", dateFormat.format(new Date())); } } 2019-04-28 17:16:09.671 INFO 6915 --- [ scheduling-1] jp.smaphonia.ststodo.ScheduledTasks : The time is now 17:16:09
定期実行する間隔はプロパティファイルで設定することも可能。fixedRateではなく、fixedRateStringになっていることに注意。
@Component @Slf4j public class ScheduledTasks { @Scheduled(fixedRateString = "${scheduler.interval}") public void reportCurrentTime() { SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); log.info("The time is now {}", dateFormat.format(new Date())); } }
application.properties
# スケジューラーインターバル scheduler.interval=60000
application.propertiesに出ているワーニングを解決すると、 src/main/java/META-INFOにadditional-spring-configuration-metadata.jsonが作成される。
{"properties": [{ "name": "scheduler.interval", "type": "java.lang.String", "description": "スケジューラーインターバル" }]}
リンク
Spring Securityでパスワードエンコーディング未実施のパスワードを使う
ちょっとしたテストでインメモリユーザーを使う場合にエラーが発生する。
java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
パスワードの前に{noop}を追加すればOK。
変更後
@Autowired public void configure(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser("user").password("{noop}password").roles("USER"); }
変更前
@Autowired public void configure(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser("user").password("password").roles("USER"); }
Spring Boot + Doma2 + SQL Serverでプロジェクト作成(メモ)
Spring Boot + Doma2 + SQL Serverでプロジェクトを作成する場合のメモです。
プロジェクト作成
Spring スターター・プロジェクトを選択
Microsoft SQL Serverの依存関係を追加
使うJavaのバージョンに合わせる。
<dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> <version>7.2.1.jre8</version> <scope>test</scope> </dependency>
最終的なpom.xml
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> <version>7.2.1.jre8</version> <scope>test</scope> </dependency> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency> </dependencies>
DataSourceを設定する。
application.properties
spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=testdb spring.datasource.username=test spring.datasource.password=test spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
接続に失敗する場合には、TCP/IP接続が有効になっているかどうか確認する。
Configuring Spring Boot for Microsoft SQL Server - Spring Framework Guru
SQL Server初期設定メモ
SQL Serverは時々しか使わないので毎回設定につまずいてしまう...orz
インストール
SQL Server ダウンロード | マイクロソフトからダウンロードする。 * SQL Server * SQL Server Management Studio
初期設定
Windows認証にSQL認証を追加する
saのプロパティ修正
TCP/IP接続できるようにする
DB作成
- SQL Server Management Toolを起動する
- ログインする(Windows認証)
- データベースを作成する
- データベース操作用アカウント作成 データベースのユーザーとデータベースへのログインユーザーの作成 (SQL Server Tips)
リンク
Vuexアプリでfirestoreアクセス
vuexで作成したWebアプリでFirestoreにアクセスする方法。
まとめ
中途半端にライブラリ使うより自前で実装した方が良さそう。
VuexFireを使う
以下の記事が参考になる。
VuexFireでNuxt.jsアプリに一瞬でFirestoreを導入する - Qiita
変数名が変わるようです。要注意! * Any example using with Nuxt.js · Issue #124 · posva/vuexfire * vuefire/CHANGELOG.md at master · vuejs/vuefire
ライブラリは使わない(お勧め)
以下の記事が参考になる。 アプリの説明を除くと結構シンプルな処理でFirestoreアクセスが可能になると思う。
vuejsのVS Code によるデバッグ
Vus.jsでWebアプリケーション開発を始めるにあたりデバッグ方法を調べました。 Vue > Visual Studio Code デバッグ - Qiitaに沿って進めるとうまくいきました。
ポイントは、2点。 vue.config.jsを作成する。
// vue.config.js module.exports = { configureWebpack: { devtool: 'source-map' } }
launch.jsを修正
"webRoot": "${workspaceFolder}/src", "breakOnLoad": true, "sourceMapPathOverrides": { "webpack:///src/*": "${webRoot}/*"
$ vue create hello-world
はリンクの通りにBPで止めることができました。 私が開発しているWebアプリケーションはtypescriptを使っているせいか、debugger文を追加しないと止めることができませんでした。
不具合修正できてめでたしめでたし。
リンク
ginでCORS対応
go(gin)で作成したWeb APIを別ドメイン(ローカルに作成したindex.html)からアクセスした場合、CORSでエラーが発生しました。
対策としては、ginで作成したハンドラでヘッダ(Access-Control-Allow-Origin)を追加すればOK。
func XXXX(ctx *gin.Context) { ctx.Header("Access-Control-Allow-Origin", "*") ctx.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE") ctx.Header("Access-Control-Max-Age", "86400") ctx.Header("Access-Control-Allow-Headers", "Access-Control-Allow-Headers, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization") ...
CORSについてもあまり良く理解していなかったので復習。 Cross-Origin Resource Sharing (CORS)は、別ドメインからアクセスした場合サーバー側で許可していないとブラウザがチェックしてエラーに してくれる。 公開されているAPIサーバーはすべてのドメインからのアクセスを許可するかわりにトークンでチェックしているのでここまで気が回っていませんでした。 実際にいろいろ試してみないと覚えることができないことを改めて痛感。