Static EJB Timer example
TweetPosted 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
Tags: ejb
Memo of useful commands
TweetPosted 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
Tags: maven
How to install a Eclipse project to local repository
TweetPosted 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
Tags: maven
Mocking a HTTP server with WireMock
TweetPosted on Saturday Feb 22, 2014 at 08:47AM in Technology
Environment
- WireMock 1.4.3
- HttpClient 4.2.3
- 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>wiremock</artifactId> <version>0.0.1-SNAPSHOT</version> <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>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-core</artifactId> <version>1.3</version> <scope>test</scope> </dependency> <dependency> <groupId>com.github.tomakehurst</groupId> <artifactId>wiremock</artifactId> <version>1.43</version> <classifier>standalone</classifier> <scope>test</scope> <exclusions> <exclusion> <groupId>*</groupId> <artifactId>*</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.3.2</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>fluent-hc</artifactId> <version>4.3.2</version> </dependency> </dependencies> </project>
HttpFetcher.java
package org.nailedtothex.wiremock; import java.io.IOException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.fluent.Request; public class HttpFetcher { public String fetchAsString(String url) throws ClientProtocolException, IOException { return Request.Get(url).execute().returnContent().asString(); } }
HttpFetcherTest.java
package org.nailedtothex.wiremock; import static com.github.tomakehurst.wiremock.client.WireMock.*; import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; import org.apache.http.client.HttpResponseException; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import com.github.tomakehurst.wiremock.junit.WireMockRule; public class HttpFetcherTest { @Rule public WireMockRule wireMockRule = new WireMockRule(18089); private HttpFetcher instance; @Before public void init() { instance = new HttpFetcher(); stubFor(get(urlEqualTo("/hoge.txt")).willReturn( aResponse().withStatus(200).withHeader("Content-Type", "text/plain").withBody("hoge"))); stubFor(get(urlEqualTo("/500.txt")).willReturn( aResponse().withStatus(500).withHeader("Content-Type", "text/plain").withBody("hoge"))); stubFor(get(urlEqualTo("/503.txt")).willReturn( aResponse().withStatus(503).withHeader("Content-Type", "text/plain").withBody("hoge"))); } @Test public void ok() throws Exception { String actual = instance.fetchAsString("http://localhost:18089/hoge.txt"); String expected = "hoge"; assertThat(actual, is(expected)); } @Test(expected = HttpResponseException.class) public void notFound() throws Exception { instance.fetchAsString("http://localhost:18089/NOT_FOUND"); } @Test(expected = HttpResponseException.class) public void internalServerError() throws Exception { instance.fetchAsString("http://localhost:18089/500.txt"); } @Test(expected = HttpResponseException.class) public void serviceUnavailable() throws Exception { instance.fetchAsString("http://localhost:18089/503.txt"); } }
Remarks
- To check behavior of the mock with browsers, we can set an breakpoint in the test class to prevent shutdown of the mock HTTP server.
- I have checked that stub urls are working as I expected with firebug.
- WireMock dependency brings many its dependencies, but I guess that are not necessary because it is classified as standalone, so I just add exclusions and it is working without any problems anyway.
References
Tags: test
How to make git commit to trigger run a Jenkins job
TweetPosted on Friday Feb 21, 2014 at 06:02PM in Technology
Environment
- Jenkins 1.551
- git version 1.8.3.4 (Apple Git-47)
- OS X 10.9.1
Try using curl
- In this setup, there is no authentication at Jenkins.
curl -X POST http://localhost:18080/job/BuildAndTestHead/build
- Let's try with dump-header option.
kyle-no-MacBook:Jenkins kyle$ curl -D - -X POST http://localhost:18080/job/BuildAndTestHead/build HTTP/1.1 201 Created Location: http://localhost:18080/queue/item/4/ Content-Length: 0 Server: Jetty(8.y.z-SNAPSHOT) kyle-no-MacBook:Jenkins kyle$
Configure a hook of repository
- Create a file named “post-commit” in .git/hooks directory.
kyle-no-MacBook:hooks kyle$ pwd /Users/kyle/gits1/hellojenkins/.git/hooks kyle-no-MacBook:hooks kyle$ ls -l total 8 -rwxr-xr-x+ 1 kyle staff 73 2 23 10:50 post-commit kyle-no-MacBook:hooks kyle$ cat post-commit #!/bin/sh curl -X POST http://localhost:18080/job/BuildAndTestHead/build kyle-no-MacBook:hooks kyle$
- Now it runs automatically when we commit using git command from cli.
Remarks
- According to [3], It doesn't work when we commit with Eclipse.
- So, we might have to config Jenkins to poll the repository periodically.
References
Tags: jenkins