Monday, August 19, 2013

How to configure SLF4J with different logger implementations

There are many good benefits in using slf4j library as your Java application logging API layer. Here I will show few examples on how to use and configure it with different loggers.
You can think of slf4j as an Java interface, and then you would need an implementation (ONLY ONE) at runtime to provide the actual logging details, such as writing to STDOUT or to a file etc. Each logging implementation (or called binding) would obviously have their own way of configuring the log output, but your application will remain agnostic and always use the same org.slf4j.Logger API. Let’s see how this works in practice.

Using slf4j with Simple logger

Create a Maven based project and this in your pom.xml.
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.5</version>
    </dependency>
Now you may use Logger in your Java code like this.
package deng;
import org.slf4j.*;
public class Hello {
    static Logger LOGGER = LoggerFactory.getLogger(Hello.class);
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++)
            if (i % 2 == 0)
                LOGGER.info("Hello {}", i);
            else
                LOGGER.debug("I am on index {}", i);
    }
}
The above will get your program compiled, but when you run it, you will see these output.
bash> java deng.Hello
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
What it’s saying is that at runtime, you are missing the logging "implementation" (or the logger binding), so slf4j simply use a "NOP" implmentation, which does nothing. In order to see the output properly, you may try use an simple implementation that does not require any configuration at all! Just go back to your pom.xml and add the following:
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.5</version>
    </dependency>
Now you see logging output on STDOUT with INFO level. This simple logger will default show any INFO level message or higher. In order to see DEBUG messages, you would need to pass in this System Property -Dorg.slf4j.simpleLogger.defaultLogLevel=DEBUG at your Java startup.

Using slf4j with Log4j logger

Now we can experiment and swap different logger implementations, but your application code can remain the same. All we need is to replace slf4j-simple with another popular logger implementation, such as the Log4j.
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.5</version>
    </dependency>
Again, we must configure logging per implementation that we picked. In this case, we need an src/main/resources/log4j.properties file.
    log4j.rootLogger=DEBUG, STDOUT
    log4j.logger.deng=INFO
    log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
    log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
    log4j.appender.STDOUT.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
Re-run your program, and you should see similar output.

Using slf4j with JDK logger

The JDK actually comes with a logger package, and you can replace pom.xml with this logger implementation.
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-jdk14</artifactId>
        <version>1.7.5</version>
    </dependency>
Now the configuration for JDK logging is a bit difficult to work with. Not only need a config file, such as src/main/resources/logging.properties, but you would also need to add a System properties -Djava.util.logging.config.file=logging.properties in order to have it pick it up. Here is an example to get you started:

.level=INFO
handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level=FINEST
deng.level=FINEST

Using slf4j with Logback logger

The logback logger implementation is a super dupa quality implementation. If you intend to write serious code that go into production, you may want to evaluate this option. Again modify your pom.xml to replace with this:
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.0.13</version>
    </dependency>
Here is a sample of configuration src/main/resources/logback.xml to get things started.
<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <logger name="deng" level="DEBUG"/>

  <root level="INFO">
    <appender-ref ref="STDOUT" />
  </root>
</configuration>

Writing your own library with slf4j logger

If you are providing an Java library for large end users consumption, it’s good idea to set your project to depend on slf4j-api only, and then let your user choose any logger implementation at their development or runtime environment. As end users, they may quickly select one of option above and take advatage of their own favorite logging implementation features.

Sunday, August 18, 2013

How to convert asciidoc text to html using Groovy

Here is how you can convert asciidoc text using Groovy script:

// filename: RunAsciidoc.groovy
@Grab('org.asciidoctor:asciidoctor-java-integration:0.1.3')
import org.asciidoctor.*
def asciidoctor = Asciidoctor.Factory.create()
def output = asciidoctor.renderFile(new File(args[0]),  [:])
println(output);

Now you may run it

groovy RunAsciidoc.groovy myarticle.txt

Many thanks to the asciidoctor.org project!

Monday, June 24, 2013

How to manage Maven third party jars

How to manage Maven third party jars

When you find yourself the need to load third party jars into Maven repository, there are few steps you normally do to test it out. You first install them locally into $HOME/.m2/repository, and then create your project pom that list those dependency. When things look good, then you deploy into your own hosted repository. The following scripts will help you perform these tasks.

Tip
If you have lot’s of jars under a group, it’s more conveninent to create an extra pom that list these dependency and install/deploy it into the repository as well. And then your project would only need to include one dependency with <type>pom</type>.
bin/mvn-install.sh
#!/usr/bin/env bash
#
# Install local jar files into Maven repository. The artifact name would be same
# as the filename minus the extension.
# :Author: Zemian Deng
# :Date: 2013/06/17
#
# Usage:
#   # Print as maven dependency used in pom file
#   mvn-install.sh mygroup 1.0.0 lib/*.jar
#
#   # Install jar files into local maven repo
#   RUN_TYPE=install mvn-install.sh mygroup 1.0.0 lib/*.jar
#
#   # Deploy jar files into remote maven repo
#   export REPO_URL=http://localhost/nexus/content/repositories/thirdparty
#   RUN_TYPE=deploy mvn-install.sh mygroup 1.0.0 lib/*.jar
#

# Capture command arguments and options
GROUP=$1
shift
VERSION=$1
shift
FILES="$@"
if [[ "$GROUP" == "" || "$VERSION" == "" || "$FILES" == "" ]]; then
 printf "ERROR: invalid arguments: GROUP VERSION FILES...\n"
 exit 1
fi

RUN_TYPE=${RUN_TYPE:="print"} # values: print|install|deploy
REPO_ID=${REPO_ID:="nexus-server"} # Id defined in user's settings.xml for authentication
REPO_URL=${REPO_URL:="http://localhost/nexus/content/repositories/thirdparty"}

# For each file, perform action based on run type.
for FILE in $FILES; do
 ARTIFACT=`basename $FILE '.jar'`
 if [[ "$RUN_TYPE" == "deploy" ]]; then
  printf "Deploying file=$FILE as artifact=$ARTIFACT to repo=$REPO_URL\n"
  mvn deploy:deploy-file \
   -DrepositoryId=$REPO_ID -Durl=$REPO_URL \
   -DgroupId=$GROUP -DartifactId=$ARTIFACT -Dversion=$VERSION -Dpackaging=jar \
   -Dfile=$FILE
 elif [[ "$RUN_TYPE" == "install" ]]; then
  printf "Installing file=$FILE as artifact=$ARTIFACT\n"
  mvn install:install-file \
   -DgroupId=$GROUP -DartifactId=$ARTIFACT -Dversion=$VERSION -Dpackaging=jar \
   -Dfile=$FILE
 elif [[ "$RUN_TYPE" == "print" ]]; then
  printf "        <dependency>\n"
  printf "            <groupId>$GROUP</groupId>\n"
  printf "            <artifactId>$ARTIFACT</artifactId>\n"
  printf "            <version>$VERSION</version>\n"
  printf "        </dependency>\n"
 fi
done

Tuesday, June 18, 2013

How to zip up a release from a hg repository

How to zip up a release from a hg repository

Did you know hg archive command can quickly zip up your project by given a revision or release name? This is very handy to package up a distribution and share with other who is refusing to use the same client.

I wrote a simple bash script to do this with couple extras. It will create a zip file with a nice basename so it’s easy for unzipping. It also auto generate and append the given revision or tag name into the RELEASE.txt file, so you know what’s been released.

Just add the following file into any root of your hg based project’s bin directory and it’s ready to use.

Note
This script will not tag your repository. It assumed you already have tagged. It simply will package up a release into a nice little zip file.
bin/zip-release.sh
#!/usr/bin/env bash
#
# Package a release or snapshot from Hg repository for distribution.
# :Author: Zemian Deng
# :Date: 2013/02/01
#
# Usage example:
#   # release a specific tag
#   cd /path/to/project
#   bin/zip-release.sh 1.0.1
#
#   # release a snapshot
#   bin/zip-release.sh
#

# Command line arguments and options
# Assume this script is in bin, which one directory up.
APP_HOME=`cd $(dirname $0)/.. && pwd`
if [[ `command -v realpath` != "" ]]; then
	# resolve symbolic link if possible.
    APP_HOME=`realpath $APP_HOME`
fi
HG_REVISION=`hg id -i`
REL_VERSION=$1
if [[ "$REL_VERSION" == "" ]]; then
    REL_VERSION=$HG_REVISION
fi
REL_NAME="`basename $APP_HOME`-$REL_VERSION"
REL_DIR=$APP_HOME/target/$REL_NAME
REL_ZIPFILE=$REL_DIR/../$REL_NAME.zip

# Generate the zip package
printf "Generating $REL_NAME in directory=`pwd`\n"
mkdir -p $REL_DIR
hg archive -r $REL_VERSION $REL_ZIPFILE

# Auto append revision id to release file.
if [[ -e $APP_HOME/RELEASE.txt ]]; then
	cp $APP_HOME/RELEASE.txt $REL_DIR/RELEASE.txt
fi
printf "$REL_NAME revsion=$HG_REVISION date=`date`\n" >> $REL_DIR/RELEASE.txt
zip -u $REL_ZIPFILE $REL_DIR/RELEASE.txt

# Clean up the tmp rel dir.
rm -r $REL_DIR

printf "$REL_ZIPFILE created.\n"

Saturday, June 15, 2013

How to install awestruct on Cygwin

How to install <code>awestruct</code> on Cygwin

How to install awestruct on Cygwin

I tried installing awestruct on Cygwin today, but it failed with following:

gem install awestruct
Building native extensions.  This could take a while...
ERROR:  Error installing awestruct:
        ERROR: Failed to build gem native extension.

        /usr/bin/ruby.exe extconf.rb
checking for libxml/parser.h... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

I am running Windows 7 with Cygwin 1.7.20 and ruby 1.9.3p392

After looking at the log and googling around, I've found that the awestruct depends on nokogiri, and in turns depends on libxslt, libxslt and iconv native lib. I have the last three already installed in Cygwin with default paths, but the problem is they are installed under /usr and not /usr/local. Because of this, I have to install the awestruct with extra parameters like this:

gem install awestruct -- --with-xml2-include=/usr/include/libxml2 \
                        --with-xml2-lib=/usr/lib \
                        --with-xslt-dir=/usr/include/libxslt \
                        --with-iconv-include=/usr/include \
                        --with-iconv-lib=/usr/lib

Now I am awestruct!

Wednesday, June 12, 2013

Taming the JMX on WebLogic Server

Taming the JMX on WebLogic Server

Let assume couple things first:

1) I assume you have heard of Java’s JMX features and familiar what it does (expose and manage your service remotely). You ought to know that default JVM will have a Platform MBeanServer instance that you can register MBean. And you may view them using the jconsole command from the JDK.

2) As of today, I think by far the easiest way you can expose any services in your application to a JMX MBeanServer is using Spring’s exporter. You will do something like this:

    <bean class="org.springframework.jmx.export.MBeanExporter">
        <property name="assembler">
              <bean class="org.springframework.jmx.export.assembler.InterfaceBasedMBeanInfoAssembler">
                <property name="managedInterfaces">
                    <list>
                        <!-- Expose any java interface you like to see under JMX as MBean -->
                        <value>myproject.services.Service</value>
                    </list>
                </property>
              </bean>
        </property>
        <property name="beans">
          <map>
            <entry key="myproject.services:name=MyCoolService" value-ref="myCoolService"/>
          </map>
        </property>
    </bean>
    <!-- This service must implements the interface used above -->
    <bean id="myCoolService" class="myproject.services.MyCoolService">
    </bean>

Above should get you standalone application with JMX enabled.

Now if you want to do similar on WebLogic Server, then I have few goodies and notes that might help you. Read on…

WebLogic Server’s (WLS) MBeanServer

The JConsole trick

The WLS, like many other EE server will have it’s own MBeanServer. However, to see the MBean’s you would need to do little extra with jconsole. Assume you have a default config WLS started on localhost, then you can connect to it like this.

jconsole -J-Djava.class.path="$JAVA_HOME/lib/jconsole.jar:$MW_HOME/wlserver/server/lib/wljmxclient.jar" -J-Djmx.remote.protocol.provider.pkgs=weblogic.management.remote

Then when prompted to login, enter the following:

Remote Process: service:jmx:iiop://localhost:7001/jndi/weblogic.management.mbeanservers.runtime
User: <same userid you used setup WLS to their console app.>
Password: <same password you used setup WLS to their console app.>

Now you should see all the MBeans that WLS already exposed to you as a EE server. You may add your own service there.

Programming with JMX connection

You may connect to the WLS MBeanServer remotely inside your standalone application. Here is a typical connection code you would need

        String serviceName = "com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean";
        try {
            ObjectName service = new ObjectName(serviceName);
        } catch (MalformedObjectNameException e) {
            throw new RuntimeException("Not able to create JMX ObjectName: " + serviceName);
        }

        String protocol = "t3";
        String jndiroot = "/jndi/";
        String mserver = "weblogic.management.mbeanservers.runtime";
        try {
            JMXServiceURL serviceURL = new JMXServiceURL(protocol, "localhost", 7001, jndiroot + mserver);

            Hashtable h = new Hashtable();
            h.put(Context.SECURITY_PRINCIPAL, username);
            h.put(Context.SECURITY_CREDENTIALS, password);
            h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,
                    "weblogic.management.remote");
            h.put("jmx.remote.x.request.waiting.timeout", new Long(10000));
            JMXConnector connector = JMXConnectorFactory.connect(serviceURL, h);
            MBeanServerConnection remoteMBeanServer = connector.getMBeanServerConnection();

            // TODO: Do what you need with remoteMBeanServer here.
        } catch (Exception e) {
            throw new RuntimeException("Not able to initiate MBeanServer protocol= " + protocol +
                    ", jndiroot= " + jndiroot + ", mserver= " + mserver);
        }

That’s a mouthful of boiler code just to get a remote MBeanServer connection! Fortunately there is another easier way though. Read on…

The JNDI trick

The WLS MBeanServer service is also available through JNDI lookup. Again Spring can help you with their JNDI lookup and you simply need to inject it to other services that need it. For example:

    <bean id="jmxServerRuntime" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:comp/env/jmx/runtime"/>
    </bean>
    <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
        <property name="beans">
            <map>
                <entry key="myproject.services:name=MyCoolService" value-ref="myCoolService"/>
            </map>
        </property>
        <property name="server" ref="jmxServerRuntime"/>
    </bean>

Notice that we have injetcted the "server" property with one we looked up from WLS JNDI service. If you use that in your WAR application and deploy it onto a WLS instance, and whola, you got yourself exposed service onto WLS JMX!

Note
above will only works if your Spring xml config is part of the WAR/JAR/EAR that’s deployed in same server where JNDI lives! If you are not, you need to use this JNDI name without the "env" part instead, like "java:comp/env/jmx/runtime".

For more details on how to work with JMX and WLS, see their docs here: http://docs.oracle.com/cd/E12839_01/web.1111/e13728/accesswls.htm#i1119556

Saturday, June 8, 2013

MySchedule 3.2.0.0 FINAL is released

Hi folks,

I have tagged the 3.2.0.0 release of the MySchedule project today. This is the first stable release of the version 3 branch line of the project. In this release we bring you the latest Vaadin UI experience. You may expect all the good stuff from old release, plus few new features added.

  • You may now save your own Templates in scheduler configurations, scripting text or even XML for job loader.
  • You may interrupt a running job.
  • More Scheduler runtime information are displayed.
  • New table display of all plugins used.
  • Added an embedded web server for quick self running UI server.
  • Single convenient package download.
  • New release format: 3.2.x.y is used for MySchedule3 using Quartz 2.x library.
  • New online demo setup on OpenShift platform

Get it and try it out here http://code.google.com/p/myschedule