Quartz-Drink-Guice

Quartz + Guice(Dependency Injection)

License

License

Categories

Categories

Net GUI User Interface Guice Application Layer Libs Dependency Injection Quartz Job Scheduling
GroupId

GroupId

net.y-yuki
ArtifactId

ArtifactId

quartz-drink-guice
Last Version

Last Version

0.1.1
Release Date

Release Date

Type

Type

jar
Description

Description

Quartz-Drink-Guice
Quartz + Guice(Dependency Injection)
Project URL

Project URL

https://github.com/YukiYoshikawa/quartz-drink-guice
Source Code Management

Source Code Management

https://github.com/YukiYoshikawa/quartz-drink-guice.git

Download quartz-drink-guice

How to add to project

<!-- https://jarcasting.com/artifacts/net.y-yuki/quartz-drink-guice/ -->
<dependency>
    <groupId>net.y-yuki</groupId>
    <artifactId>quartz-drink-guice</artifactId>
    <version>0.1.1</version>
</dependency>
// https://jarcasting.com/artifacts/net.y-yuki/quartz-drink-guice/
implementation 'net.y-yuki:quartz-drink-guice:0.1.1'
// https://jarcasting.com/artifacts/net.y-yuki/quartz-drink-guice/
implementation ("net.y-yuki:quartz-drink-guice:0.1.1")
'net.y-yuki:quartz-drink-guice:jar:0.1.1'
<dependency org="net.y-yuki" name="quartz-drink-guice" rev="0.1.1">
  <artifact name="quartz-drink-guice" type="jar" />
</dependency>
@Grapes(
@Grab(group='net.y-yuki', module='quartz-drink-guice', version='0.1.1')
)
libraryDependencies += "net.y-yuki" % "quartz-drink-guice" % "0.1.1"
[net.y-yuki/quartz-drink-guice "0.1.1"]

Dependencies

compile (3)

Group / Artifact Type Version
org.quartz-scheduler : quartz jar 2.2.3
com.google.inject : guice jar 3.0
javax.inject : javax.inject jar 1

Project Modules

There are no modules declared in this project.

quartz-drink-guice

Quartz + Guice(Dependency Injection)

Getting started

Add dependency in your pom.xml or other build tool's configuration file.

<dependency>
    <groupId>net.y-yuki</groupId>
    <artifactId>quartz-drink-guice</artifactId>
    <version>0.1.1</version>
</dependency>

Define a job class

import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import javax.inject.Inject;
import javax.inject.Singleton;


@DisallowConcurrentExecution
@Singleton
public class MyJob implements org.quartz.Job {
    
    private final MyLogic logic;

    @Inject
    public MyJob(MyLogic logic) {
        this.logic = logic;
    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        logic.invoke();
    }
}
import com.google.inject.ImplementedBy;

@ImplementedBy(TrialQuartzImpl.class)
public interface TrialQuartz {

    void run();
}
import net.yyuki.quartz.ex.guice.QuartzEx;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;

import javax.inject.Inject;
import javax.inject.Singleton;

import static org.quartz.SimpleScheduleBuilder.simpleSchedule;

@Singleton
public class TrialQuartzImpl implements TrialQuartz {

    private final QuartzEx quartz;

    @Inject
    public TrialQuartzImpl(QuartzEx quartz) {
        this.quartz = quartz;
    }

    @Override
    public void run() {
        JobDetail job1 = JobBuilder.newJob(MyJob.class)
                .withIdentity("job1", "group1")
                .build();

        Trigger trigger1 = TriggerBuilder.newTrigger()
                .withIdentity("trigger1", "group1")
                .startNow()
                .withSchedule(simpleSchedule()
                        .withIntervalInMilliseconds(100)
                        .repeatForever())
                .build();

        Scheduler scheduler = quartz.getScheduler();
        try {
            scheduler.start();
            scheduler.scheduleJob(job1, trigger1);
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }


        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        try {
            scheduler.shutdown();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}

DI Container start

import com.google.inject.Guice;
import com.google.inject.Injector;
import net.yyuki.quartz.ex.guice.QuartzModule;

public class QuartzTrialMain {

    public static void main(String[] args) throws Exception {
        Injector injector = Guice.createInjector(new QuartzModule());

        TrialQuartz trialQuartz = injector.getInstance(TrialQuartz.class);

        trialQuartz.run();
    }
}

Versions

Version
0.1.1
0.1.0