LoginSignup
45
47

More than 5 years have passed since last update.

SpringBootでバッチアプリ

Last updated at Posted at 2015-08-23

SpringBootでバッチアプリが簡単に作れます

プロパティファイルなどを共有できるので1つのプロジェクトに両方入れちゃう事ができます。
スモールビジネスの場合に大活躍です。

サンプル

1.CommandLineRunnerを継承します。

2.@EnableAutoConfigurationを追加します。
(他のやつはなくても動くかも・・・・)

3.ApplicationContext@Autowiredしておきます。
new しなくても勝手に中身が入ってます。

@Autowired
private ApplicationContext context;

4.自クラスをSpringApplicationにぶち込みます。

SpringApplication application = new SpringApplication(MongExample.class);

5.内蔵ウェブサーバーの立ち上げをOFFにします。

application.setWebEnvironment(false);

6.コンテキストを渡して処理を走らせます

ApplicationContext context = application.run();

7.処理が終わったらEXITします

SpringApplication.exit(context);

別スレッドでrunメソッドが走ってるので、ここはシンクロナイズドしないといけないかも???
Handlerで終了を受け取ってもいいと思います。
ただ、exitしないとずっとjarが起動したまんま終了しないんですね。
その代わり、この中でタイマー回しておく事ができるので、数分に1回処理をしないといけないとか監視系の処理は立ち上げっぱなしでOK!

MongExample.java
@EnableAutoConfiguration
@EnableBatchProcessing
@EnableConfigurationProperties
public class MongExample implements CommandLineRunner {

    //  get Applicateion context Example2
    @Autowired
    private ApplicationContext context;

    @Autowired
    private Environment env;

    public static void main(String[] args) {
        //SpringApplication.run(MongExample.class, args);

        SpringApplication application = new SpringApplication(MongExample.class);
        application.setWebEnvironment(false);
        ApplicationContext context = application.run();
        SpringApplication.exit(context);


    }

    @Override
    public void run(String... args) throws Exception {
        //  get mongo Template
        MongoOperations mongoOperation = (MongoOperations) context.getBean("mongoTemplate");

        //  Data Object this structure can be a table
        User user = new User("mkyong", "password123");

        // save
        mongoOperation.save(user);

        // now user object got the created id.
        System.out.println("1. user : " + user);

        // query to search user
        Query searchUserQuery = new Query(Criteria.where("username").is("mkyong"));

        // find the saved user again.
        User savedUser = mongoOperation.findOne(searchUserQuery, User.class);
        System.out.println("2. find - savedUser : " + savedUser);

        // update password
        mongoOperation.updateFirst(searchUserQuery, 
                            Update.update("password", "new password"),User.class);

        // find the updated user object
        User updatedUser = mongoOperation.findOne(searchUserQuery, User.class);

        System.out.println("3. updatedUser : " + updatedUser);

        // delete
        //mongoOperation.remove(searchUserQuery, User.class);

        // List, it should be empty now.
        List<User> listUser = mongoOperation.findAll(User.class);
        System.out.println("4. Number of user = " + listUser.size());

    }
}
45
47
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
45
47