Kohei Nozaki's blog 

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)


Memo of useful commands


Posted on Monday Feb 24, 2014 at 10:53AM in Technology


Environment

  • Apache Maven 3.1.1

Common goals

clean

  • Delete everything at target directory

compile

  • Compile sources to class files

test

  • Run JUnit test classes

package

  • Make the JAR file or WAR file

install

  • It does “package” goal and install the package to local repository so that other Maven project can reference it.

Execute Main class

mvn exec:java -Dexec.mainClass=org.sonatype.mavenbook.weather.Main -Dexec.args="70112"

Show dependencies

Command

mvn dependency:resolve

What will we got

[INFO] The following files have been resolved:
[INFO]    com.ibm.icu:icu4j:jar:2.6.1:compile
[INFO]    xml-apis:xml-apis:jar:1.0.b2:compile
[INFO]    xerces:xmlParserAPIs:jar:2.6.2:compile
[INFO]    oro:oro:jar:2.0.8:compile
[INFO]    log4j:log4j:jar:1.2.14:compile
[INFO]    velocity:velocity:jar:1.5:compile
[INFO]    dom4j:dom4j:jar:1.6.1:compile
[INFO]    commons-lang:commons-lang:jar:2.1:compile
[INFO]    xerces:xercesImpl:jar:2.6.2:compile
[INFO]    commons-collections:commons-collections:jar:3.1:compile
[INFO]    junit:junit:jar:3.8.1:test
[INFO]    jdom:jdom:jar:1.0:compile
[INFO]    xalan:xalan:jar:2.6.0:compile
[INFO]    jaxen:jaxen:jar:1.1.1:compile
[INFO]    commons-io:commons-io:jar:1.3.2:test
[INFO]    xom:xom:jar:1.0:compile

Show dependencies in tree style

Command

mvn dependency:tree

What will we got

[INFO] org.sonatype.mavenbook.custom:simple-weather:jar:0.8-SNAPSHOT
[INFO] +- log4j:log4j:jar:1.2.14:compile
[INFO] +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  \- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] +- jaxen:jaxen:jar:1.1.1:compile
[INFO] |  +- jdom:jdom:jar:1.0:compile
[INFO] |  +- xerces:xercesImpl:jar:2.6.2:compile
[INFO] |  \- xom:xom:jar:1.0:compile
[INFO] |     +- xerces:xmlParserAPIs:jar:2.6.2:compile
[INFO] |     +- xalan:xalan:jar:2.6.0:compile
[INFO] |     \- com.ibm.icu:icu4j:jar:2.6.1:compile
[INFO] +- velocity:velocity:jar:1.5:compile
[INFO] |  +- commons-collections:commons-collections:jar:3.1:compile
[INFO] |  +- commons-lang:commons-lang:jar:2.1:compile
[INFO] |  \- oro:oro:jar:2.0.8:compile
[INFO] +- commons-io:commons-io:jar:1.3.2:test
[INFO] \- junit:junit:jar:3.8.1:test

Verbose Output

Command

mvn install -X

What will we got

  • It outputs tons of verbose information, such as why each jars are included or excluded.

How to skipping unit tests

mvn install -Dmaven.test.skip=true

Show description of a plugin

mvn help:describe -Dplugin=exec -Dfull

Create a jar with dependencies

mvn assembly:attached
  • This might need additional configuration of the plugin in the pom.xml

Run Jetty and deploy the WAR

mvn jetty:run
  • This might be useful for some proto-typing of servlet or jsp.
  • This might need additional configuration of the plugin in the pom.xml

Project Hierarchy

  • Every Maven projects can have children or its parent.
  • It's useful for declare common dependencies (using dependencyManagement) or plugins (using pluginManagement).
    • Child pom.xml can omit “version” element in dependencies that are declared in dependencyManagement of the parent pom.xml

Analyze “used undeclared” or “unused declared” dependencies

mvn dependency:analyze


How to install a Eclipse project to local repository


Posted on Sunday Feb 23, 2014 at 04:51PM in Technology


Environment

  • Apache Maven 3.1.1
  • Eclipse Kepler SR1
  • Oracle JDK7u51
  • OS X 10.9.1

Why need to do it?

  • Many kind of dependency resolving work can take on Maven.
  • It is a better way than that take it on Eclipse or IDEs.
    • It is stable and it has some efficient mechanism like local repository.
    • We can just put in Jenkins job or shell scripts.
    • It makes automation of some annoying works (e.g. build, deploy, etc) much easier.
  • Thus, in this post, I'm going to try to resolve a dependency through local repository of Maven.

Create a project

  • I'm going to install this project to local repository.
  • After install, I will try to resolve a dependency to it from another project.

Create a project

  • Create a maven project named “hogeproject” in Eclipse.
    • Check “Create a simple project (skip archetype selection)”

Edit 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>hogeproject</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

Make a class

package hogeproject;

public class Hello {

    public String hello() {
        return "hello from hogeproject";
    }
}

Install the project to Maven repository

kyle-no-MacBook:hogeproject kyle$ pwd
/Users/kyle/Documents/workspace/hogeproject
kyle-no-MacBook:hogeproject kyle$ ls -l
total 8
-rw-r--r--+ 1 kyle  staff  566  2 23 17:07 pom.xml
drwxr-xr-x+ 4 kyle  staff  136  2 23 17:01 src
drwxr-xr-x+ 7 kyle  staff  238  2 23 17:12 target
kyle-no-MacBook:hogeproject kyle$ mvn install
Picked up _JAVA_OPTIONS: -Dfile.encoding=UTF-8
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building hogeproject 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hogeproject ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ hogeproject ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hogeproject ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ hogeproject ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hogeproject ---
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hogeproject ---
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ hogeproject ---
Downloading: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.pom
Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.pom (3 KB at 2.3 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.jar
Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.5/plexus-utils-3.0.5.jar (226 KB at 193.3 KB/sec)
[INFO] Installing /Users/kyle/Documents/workspace/hogeproject/target/hogeproject-0.0.1-SNAPSHOT.jar to /Users/kyle/.m2/repository/org/nailedtothex/hogeproject/0.0.1-SNAPSHOT/hogeproject-0.0.1-SNAPSHOT.jar
[INFO] Installing /Users/kyle/Documents/workspace/hogeproject/pom.xml to /Users/kyle/.m2/repository/org/nailedtothex/hogeproject/0.0.1-SNAPSHOT/hogeproject-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.438s
[INFO] Finished at: Sun Feb 23 17:19:25 JST 2014
[INFO] Final Memory: 11M/245M
[INFO] ------------------------------------------------------------------------
kyle-no-MacBook:hogeproject kyle$ 
  • It showed that the jar file and pom.xml were placed somewhere under $HOME/.m2/repository.

Create an another project

  • Create in Eclipse same way as above. Name is “higeproject”

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>higeproject</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <failOnMissingWebXml>false</failOnMissingWebXml>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.nailedtothex</groupId>
            <artifactId>hogeproject</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>
  • A dependency to hogeproject is specified.

Build the package by Maven

kyle-no-MacBook:higeproject kyle$ ls -l
total 8
-rw-r--r--+ 1 kyle  staff  825  2 23 17:27 pom.xml
drwxr-xr-x+ 4 kyle  staff  136  2 23 17:23 src
drwxr-xr-x+ 9 kyle  staff  306  2 23 17:27 target
kyle-no-MacBook:higeproject kyle$ pwd
/Users/kyle/Documents/workspace/higeproject
kyle-no-MacBook:higeproject kyle$ mvn clean package
Picked up _JAVA_OPTIONS: -Dfile.encoding=UTF-8
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building higeproject 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ higeproject ---
[INFO] Deleting /Users/kyle/Documents/workspace/higeproject/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ higeproject ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ higeproject ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ higeproject ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ higeproject ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ higeproject ---
[INFO] 
[INFO] --- maven-war-plugin:2.2:war (default-war) @ higeproject ---
[INFO] Packaging webapp
[INFO] Assembling webapp [higeproject] in [/Users/kyle/Documents/workspace/higeproject/target/higeproject-0.0.1-SNAPSHOT]
[INFO] Processing war project
[INFO] Copying webapp resources [/Users/kyle/Documents/workspace/higeproject/src/main/webapp]
[INFO] Webapp assembled in [23 msecs]
[INFO] Building war: /Users/kyle/Documents/workspace/higeproject/target/higeproject-0.0.1-SNAPSHOT.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.106s
[INFO] Finished at: Sun Feb 23 17:31:39 JST 2014
[INFO] Final Memory: 9M/245M
[INFO] ------------------------------------------------------------------------
kyle-no-MacBook:higeproject kyle$ unzip -l target/
classes/                        higeproject-0.0.1-SNAPSHOT/     higeproject-0.0.1-SNAPSHOT.war  maven-archiver/                 test-classes/
kyle-no-MacBook:higeproject kyle$ unzip -l target/higeproject-0.0.1-SNAPSHOT.war 
Archive:  target/higeproject-0.0.1-SNAPSHOT.war
  Length     Date   Time    Name
 --------    ----   ----    ----
        0  02-23-14 17:31   META-INF/
      129  02-23-14 17:31   META-INF/MANIFEST.MF
        0  02-23-14 17:31   WEB-INF/
        0  02-23-14 17:31   WEB-INF/classes/
        0  02-23-14 17:31   WEB-INF/lib/
     1986  02-23-14 17:12   WEB-INF/lib/hogeproject-0.0.1-SNAPSHOT.jar
        0  02-23-14 17:31   META-INF/maven/
        0  02-23-14 17:31   META-INF/maven/org.nailedtothex/
        0  02-23-14 17:31   META-INF/maven/org.nailedtothex/higeproject/
      825  02-23-14 17:27   META-INF/maven/org.nailedtothex/higeproject/pom.xml
      121  02-23-14 17:31   META-INF/maven/org.nailedtothex/higeproject/pom.properties
 --------                   -------
     3061                   11 files
kyle-no-MacBook:higeproject kyle$ 
  • We can see that the WAR contains “hogeproject-0.0.1-SNAPSHOT.jar”.

References

  1. Maven - Running Maven


How to make a Jenkins job to poll SCM periodically


Posted on Sunday Feb 23, 2014 at 11:11AM in Jenkins


Environment

  • Jenkins 1.551
  • git version 1.8.3.4 (Apple Git-47)
  • OS X 10.9.1

Why need it?

  • As I wrote in How to make git commit to trigger run a Jenkins job, Git plugin of Eclipse doesn't fire hooks.
    • Unfortunately, currently EGit not supported it, and maybe will not[1].
  • So we need an alternative way, such as make Jenkins to poll SCM periodically.
    • It seems to I can configure it easily so I just try it here.

How to configure

  1. Go to configure page of a project.
  2. Go to “Build Triggers” section.
  3. Check “Poll SCM”
  4. Enter the schedule that you want in cron-style.
    • The help that placed side of input area might be useful
    • Example for poll at every 15minutes is here:
  5. Save

See the log

  • When any change is detected at polling, we can see that message in log of Jenkins.
    • In this setup, the log file is located at /var/log/jenkins/jenkins.log
Feb 23, 2014 11:24:45 AM hudson.triggers.SCMTrigger$Runner run
情報: SCM changes detected in BuildAndTestHead. Triggering  #22
Feb 23, 2014 11:24:57 AM hudson.model.Run execute
情報: BuildAndTestHead #22 main build action completed: SUCCESS

Remarks

  • I'm not sure that CPU usage or any other resource consumption of git polling.

References

  1. Eclipse Community Forums: EGit » Enabling hooks in EGit


How to send Email at every build with Jenkins


Posted on Saturday Feb 22, 2014 at 09:34PM in Jenkins


Environment

  • Email-ext plugin 2.37.2
  • Jenkins 1.551
  • Apache Maven 3.1.1
  • git version 1.8.3.4 (Apple Git-47)
  • Oracle JDK7u51
  • OS X 10.9.1

Install Email-ext plugin

  • Install Email-ext plugin at plug-in install page of Jenkins

Configure System

“Jenkins Location” section

  1. Enter valid email address to “System Admin e-mail address”

“Extended E-mail Notification” section

  1. Enter your email address to “Default Recipients”

“E-mail Notification” section

  1. Enter your SMTP server name to “SMTP server”
  2. Click “Advanced”
  3. Click “Use SMTP Authentication”
  4. Enter required informations
  5. Check “Test configuration by sending test e-mail”
  6. Click “Test configuration” to send test email
  7. Click “Save” in the bottom of the page

Configure a project to send email at every build

  1. Click “Add post-build action”
  2. Click “Editable Email Notification”
  3. Click “Advanced Settings…”
  4. Click “Add Trigger”
  5. Click “Always”
  6. Save

Test-run

  1. Click “Build Now”
  2. Check Console output and received email
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.497s
[INFO] Finished at: Sat Feb 22 22:27:21 JST 2014
[INFO] Final Memory: 18M/245M
[INFO] ------------------------------------------------------------------------
[JENKINS] Archiving /Users/Shared/Jenkins/Home/jobs/BuildAndTestHead/workspace/hellojenkins/pom.xml to org.nailedtothex/hellojenkins/0.0.1-SNAPSHOT/hellojenkins-0.0.1-SNAPSHOT.pom
channel stopped
Archiving artifacts
Email was triggered for: Always
Sending email for trigger: Always
Sending email to: kyle@example.com
Finished: SUCCESS

References

  1. Email-ext plugin - Jenkins - Jenkins Wiki