Kohei Nozaki's blog 

Job-wide artifact injection with CDI Producer


Posted on Tuesday Feb 25, 2014 at 05:37PM in Technology


Environment

  • jBeret 1.0.1Beta-SNAPSHOT
  • WildFly 8.0.0.Final

Why need it?

  • Logger injection through CDI is easy and useful, but for jBatch programming of some occasions, I guess that Job-wide Logger is better than typical class-wide logger. thus, I will try it this time.
  • With CDI, we can reduce some annoying code, even related to Job Properties so I also will try to inject a job-level property to a Batchlet through Producer.

Sample project

How does it work?

  • 2 Injections are declared in InjectBatchlet.
  • Both of them will be produced by JobWideArtifactProducer.
  • JobWideArtifactProducer creates:
    • a logger. the name contains job name.
    • a Date. it came from job-level property named “baseDate”.

Log

18:14:51,064 INFO  [job.jobwideproducer] (batch-batch - 3) process(): baseDate=14/02/25 0:00

Remarks

  • Injection of variables such as working directory of the job may be useful too.

References

  1. [Java] CDI - Slf4j Logger Producer
  2. SimpleDateFormat (Java Platform SE 6)


Static EJB Timer example


Posted on Tuesday Feb 25, 2014 at 04:08PM in Technology


Environment

  • WildFly8.0.0.Final
  • Oracle JDK7u51

Resources

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.nailedtothex</groupId>
    <artifactId>ejbtimer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
     <packaging>ejb</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

</project>

TimerService.java

package org.nailedtothex.ejbtimer;

import javax.ejb.Schedule;
import javax.ejb.Singleton;

@Singleton
public class TimerService {

    @Schedule(second = "*/1", minute = "*", hour = "*", persistent = false)
    public void periodic() {
        System.out.println("period");
    }

    @Schedule(dayOfMonth = "25", hour = "16", minute = "10", persistent = false)
    public void schedule() {
        System.out.println("schedule");
    }
}
  • Almost all of the code was taken from [1].
  • There are some additional useful informations and conversations so I recommend to visit [1].
  • According to [2], “persistent” attribute means whether timers that missed should fire or not during downtime of the application server.
    • Default is true.
    • I don't want many missed timers to fire at once when I launch the application server which stopped unexpectedly so I just set it false.
  • Detail of specification of @Schedule here: [4]

Log

16:09:44,002 INFO  [stdout] (EJB default - 8) period
16:09:45,001 INFO  [stdout] (EJB default - 9) period
16:09:46,002 INFO  [stdout] (EJB default - 10) period
16:09:47,002 INFO  [stdout] (EJB default - 1) period
16:09:48,002 INFO  [stdout] (EJB default - 2) period
16:09:49,002 INFO  [stdout] (EJB default - 4) period
16:09:50,002 INFO  [stdout] (EJB default - 3) period
16:09:51,002 INFO  [stdout] (EJB default - 5) period
16:09:52,002 INFO  [stdout] (EJB default - 6) period
16:09:53,002 INFO  [stdout] (EJB default - 7) period
16:09:54,002 INFO  [stdout] (EJB default - 8) period
16:09:55,002 INFO  [stdout] (EJB default - 9) period
16:09:56,001 INFO  [stdout] (EJB default - 10) period
16:09:57,001 INFO  [stdout] (EJB default - 1) period
16:09:58,001 INFO  [stdout] (EJB default - 2) period
16:09:59,002 INFO  [stdout] (EJB default - 4) period
16:10:00,002 INFO  [stdout] (EJB default - 5) period
16:10:00,003 INFO  [stdout] (EJB default - 3) schedule
16:10:01,001 INFO  [stdout] (EJB default - 6) period
16:10:02,002 INFO  [stdout] (EJB default - 7) period
16:10:03,001 INFO  [stdout] (EJB default - 8) period
16:10:04,001 INFO  [stdout] (EJB default - 9) period
16:10:05,001 INFO  [stdout] (EJB default - 10) period
16:10:06,002 INFO  [stdout] (EJB default - 1) period
16:10:07,003 INFO  [stdout] (EJB default - 2) period

References

  1. Simplest Possible EJB 3.1 Timer : Adam Bien's Weblog
  2. ejb 3.0 - EJB3 + JEE6: What is a persistent Timer? - Stack Overflow
  3. MaestroDev | Building an EJB Project
  4. Schedule (Java™ EE 7 Specification APIs)