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

Wednesday, June 5, 2013

Another myschedule-3.0.0_q21-SNAPSHOT.4

I have uploaded yet another snapshot of latest MySchedule 3. This should be the last snapshot before FINAL tag. All the features are complete! Please try it out and let me know.

http://code.google.com/p/myschedule/downloads/list

Saturday, June 1, 2013

Second snapshot of the latest MySchedule 3 is out

Second snapshot of the latest MySchedule 3 is out.

  • The SchedulerScreen now has tab sheet view
  • Added JobWithoutTrigger view
  • Added Current Job Execution view
  • Added Scheduler Status view
  • Added Save Template into File

Get the download and try it out: http://code.google.com/p/myschedule/downloads/list

Thursday, May 30, 2013

Choosing Technology Stack to build a common platform

There are many talks in Java community about Spring vs Java EE, and how one group would argue you should use one and not other etc. When I see this, I can't help but to think why can't we use them both together? In fact I think using them both effectively would create a great technology stack for building an infrastructure for large company who like to provide a common platform that may host and run many different applications and projects.

Why combine Spring and Java EE together?

When writing software, we create and build libraries/framework that can be reuse and help us get things done faster. Spring is such a swiss-army knife like library that allows application to be build in an un-intrusive way and with many easier wrappers and helpers classes. This is what I call developer friendliness library.

However many people don't realize is that Spring is just a library and wrappers to many things that ease the development. Spring has a web framework layer to let you write MVC web app, but you still need a Servlet Container (server). Spring provides data layer that mostly wraps other JPA/Hibernate/JDBC that integrate well in their IoC container, but the actual ORM implementation is outside of Spring (eg: Hibernate). Spring wraps JMS or even JNDI for development, but you still need a JMS server or JNDI provider. Spring has a Transaction Manager Abstract layer, but it's just a wrapper (for single DB, it's the database vendor that actually provided the ACID attributes of your transaction guarantee, not Spring). If you want to atomic transaction on multiple resources (such as JMS and Database, or multiple databases) you still need a "real" Transaction Manager (JTA)!

So now you see, in order to build a successful enterprise application, you need Spring on top of many vendor provided features. If you are not careful, you could be lock into many proprietary services that's hard to integrate and deploy. This is where Java EE comes in. It's a spec layout that vendor must provide most of those services in a standard manner. Hence any JEE compliance server would provide services with standard API that suppose to works the similar way.

Now there must be balance to make. From my experience, the more standards you enforce, then less "developer friendliness" it will get. But at the same time, without standard, it's hard to provide common infrastructure such as API, runtime server and/or even OS environment for deployment. This is the reason I would argue that combining Spring with Java EE would bring most practical and effective platform to IT.

Choosing the Technology Stack

Not every project is created equals and their needs are different. So providing a common technology stack that will satisfy all projects is impossible. But we can certainly try to create a common one that satisfy most of projects need. Also choosing a concrete library/stack is very opinionated, and no matter which actual implementation is selected, there are always going to be pro and cons. With this in mind, I will try to layout my own personal choices of a technology stack that I think it will provide the most flexible platform to host majority projects and applications. Specially in a big corporate environment.

I will choose a Java EE application server as common platform. From this, I will choose some "developer friendliness" libraries that replace (or add on-top) few existing EE standards to gain productivity. I think EE has come a long way and getting better through each spec iteration, but I still feel it is more flexibility in using Spring as IoC container verse using CDI when wiring POJO services together. Plus the Spring comes with a very flexible MVC framework layer that's effective and easy to development in compare to plain Servlet API. Once these are combined and available as a common platform, I think it can support many kind of applications in various IT environment.

Starting Java EE 6, there are two profiles a server must provide now. So let's start exploring the stack from these two views.

JEE Web profile - Lighter web based driven application

  • Use Spring MVC (controller, form, validation, ModelAndView and IoC configurations) instead of plain Servlet API programming.
  • Write backend business service logic as POJO as possible and use Spring IoC to wire them. Do not abuse this. I personally think Spring IoC is more flexible and easier to use in compare to CDI.
  • Use JPA for data service layer instead of JDBC API programming.
  • Use JSON data exchange format. From experience, JSON is much more efficient and easier to work in comparison to XML.
  • Views options:
    • Use well formed xhtml/Bootstraps/jquery/AJAX -> If you need just static pages and some client side interactive
    • Freemarker (or Thymeleaf) -> If you need a lot of dynamic content to generate, use an template engine! This is better than JSP alone.
    • Vaadin -> If you need desktop application behavior like on web browser side. This is easier in compare to JSF.
  • Servlet 3.0 now support asynchronous requests. This solve many challenging problems in web domain. Take advantage of it if needed! (Latest Spring MVC has support for this already.)

JEE 6 Full profile - Full EE features application

  • On top of everything mentioned above in Web Profile.
  • Use JMS for any messaging need that fall into Point-To-Point or Publish/Subscribe domain.
  • Use JTA when you need atomic transaction for multiple databases and/or JMS delivery.
  • Use standard JAX-RS (RESTful web service API) for exposing external services. Use JSON data exchange format.
  • Use consistent Spring IoC for services injection. It's more flexible and easier to work with in compare to CDI.
  • Use POJO services and wire by Spring instead of EJB if possible. I found them more easier to test and development.
  • Plus whatever else EE spec somes with that it supports such as (JavaMail and JCA etc. usually Spring will have a easier wrapper for these API as well.)

Apache Camel - Light Weight ESB

The Camel project is not an EE standard, however from my experience is that many common IT work can be easily done with simple Camel route/workflow. The development and style of Camel is simple to understand and easy to test. It can be run as stand alone application/service or as part of a web application. I believe it's a great value to add on top of a common platform with above. You would use it whenever you need the following:

  • For any integration pattern like work flow process (eg: bridge a file poller to a web service to a JMS queue to database etc.)
  • For creating business workflow process.
  • For any ETL workflow process.
  • For easy mapping of business requirement workflow to code logic process

Which EE application server to use

I think this is subjective as well, but we need to choose one that's fit for business need. I personally favor JBoss because it's open source, and yet they provide commercial backed version of their application server. Being an open source based product, it gives developers greater flexibility in learning and exploring the platform. I also see many benefits in their in house projects such as testing tools and libraries are open and benefit to developers.

What about Tomcat server?

Tomcat is a very well known Servlet server. However it is only a Web container that support Servlet/JSP application. It does not provide JMS or JTA features that provided by a Java EE server. It's a fact that many web application requirements can be satisfy with simple Tomcat server. However in a large IT env, you will often you need other services that provided by Java EE server. In many cases, people will simply run Tomcat webapp and bridge to other Java EE server when needed.

Well, this is reason I would select a Java EE server in the first place. Specially with JEE6, it provided Web Profile that stripped down to mostly Web Container features, then should make the server faster to start-up and less extra services loaded. However, in case when application needed EE features, the platform we provide will able to support it.