Help us understand the problem. What is going on with this article?

SpringBootでバッチアプリ

More than 3 years have passed since last update.

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());

    }
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした