Use SpringBoot to Deploy ActiveMQ Broker to Tomcat 3/3

ActiveMQ supports LeaseDatabaseLocker in its recent releases to increase its reliability when handling database failover. With the previous default database locker, the master broker holds the lock until it initiates a request to release the lock. This is problematic when database fails or the network is disconnected abruptly. In such cases, the master can not properly release the lock anymore therefore neither the master nor the slave, until the brokers are restarted, can own the lock after the database comes back. This is often not desirable in production environment.

LeaseDatabaseLocker is implemented to overcome this weakness. With this, the master essentially creates a row in the activemq_lock table and uses it as a lock. The lock is only effective for a period of time. If it is not renewed/refreshed before expiry, any broker can take over the lock ownership when the database becomes accessible.

The code to configure the LeaseDatabaseLocker looks like

  public PersistenceAdapter persistenceAdapter(DataSource dataSource,
      @Value("${}") String brokerName) throws Exception {
    JDBCPersistenceAdapter jdbcPersistenceAdapter =
        new JDBCPersistenceAdapter(dataSource, new OpenWireFormat());
    LeaseDatabaseLocker locker = new LeaseDatabaseLocker();
    // TODO: externalize the value into
    //This value must be greater than the LockKeepAlivePeriod
    // TODO: externalize the value into
    return jdbcPersistenceAdapter;

Notice how the lock holding period and the refresh period are configured by the setLockAcquireSleepInterval() and setLockKeepAlivePeriod() respectively.

Please be aware that you’d better to use DBCP2 connection pool to manage the data source connections for this application. If Tomcat pool manager is used, you may experience database blocking problem. The full configuration can be found from the source code.

You can find the two earlier posts related to this one: part1 and part2.

Posted in Uncategorized | Tagged , , , , , | 2 Comments

Hibernate Fetch Lazy Child Entities with HQL and Native SQL

In one-to-many or many-to-many relationship, we often configure Hibernate to lazily fetch the children collection for performance reasons by reducing the database join operation and the amount of the data been pulled and transmitted from database to application. The application, if needs the child entities, can query the database again. This is the typical n+1 operation in Hibernation. When the data size is small, you won’t notice too much performance problem. However, this can impose some undesired issue with the O(n2) operations as data grows.

Fortunately Hibernate provides a few ways to fetch those lazy entities in much efficient way, with Criteria, HQL or native SQL. You can refer the official document for comprehensive guide.

This post will simply provides two examples, using HQL and native SQL. The entity in the following sections means Java beans mapped by Hibernate with @Entity or xml configuration. The non-entity means plain Java Bean without Hibernate awareness.

Example 1: Using HQL to fetch parents and their children into entities
This example fetches data into a Hibernate aware entity bean Order, which has a set of OrderItems.

  public List<Order> getOrdersByItemName(String accountNumber,String itemName) {      
    Query query = currentSession.createQuery(
        "from Order o " 
        + "join fetch o.orderItems i "
        + "where o.accountNumber = :accountNumber and = :itemName" 
        + "order by asc"
    ).setParameter("accountNumber", accountNumber)
     .setParameter("itemName", itemName)
    return query.list();

Example 2: Using native HQL to fetch parents and their children into non-entities
This examples converts the query results into a non-entity bean FlattenedOrder. It demonstrates how complicated the native query can go (using SqlServer). It also shows how to map two column values to two user defined Enums OrderSource and ItemSize.

  public List<FlattenedOrder> getFlattenedOrderByItemName(String accountNumber, String itemName) {
    String queryString =
        "select distinct o.orderIdStr as orderId, o.orderSource, o.orderDate,o.expiryDate "
            + "CASE WHEN o.expiryDate <= GETDATE() THEN 0 ELSE DATEDIFF(day, o.orderDate, o.expiryDate) END as daysExpiryIn, "
            + " as itemName, i.size as itemSize, "
            + "(select sum(innerI.quantity) from dbo.OrderItem innerI join dbo.Order innerO on where as total, "
            + "from dbo.Order as o "
            + "join OrderItem as i on "
            + "where o.accountNumber=:accountNumber and = : itemName";
	Properties orderSourceParams = new Properties();
    orderSourceParams.put("enumClass", "ca.zl.OrderSource");
    orderSourceParams.put("type", "12"); /*EnumType.STRING type = 12 */
    Type orderSourceEnumType = new TypeLocatorImpl(new TypeResolver()).custom(EnumType.class, orderSourceParams);
    Properties orderItemSizeProps = new Properties();
    orderItemSizeProps.put("enumClass", "ca.zl.ItemSize");
    orderItemSizeProps.put("type", "12"); /*EnumType.STRING type = 12 */
    Type  itemSizeEnumType= new TypeLocatorImpl(new TypeResolver()).custom(EnumType.class, orderItemSizeProps);
    SQLQuery query = currentSession.createSQLQuery(queryString);
        query.setParameter("accountNumber", accountNumber);
		query.setParameter("itemName", itemName);
        query.addScalar("orderId", new StringType())
        .addScalar("orderSource", orderSourceEnumType)
        .addScalar("orderDate", new DateType())
        .addScalar("expiryDate", new DateType())
        .addScalar("daysExpiryIn", new IntegerType())
        .addScalar("itemName", new StringType())
		.addScalar("itemSize", itemSizeEnumType)
        .addScalar("total", new IntegerType())
    return query.list();
Posted in Database, Java | Tagged , , , , , , , , | Leave a comment

Use Hamcrest Matchers in JMockIt

JMockIt, when used together with Hamcrest matchers, can sometimes create very flexible verifications in unit test. This is demonstrated in the gist here.

The test class GameConrollerTest defines a test target (with @Tested) and a mocked object (in this case it is a class only with static methods, with @Mocked). In the only test method testCreatPerson(), it sets up an expectation, followed by invoking the target method under test, then finally provides a verification block to assert that some behaviors from the mocked object(class) have been invoked. The verification code snippet is provided below

    new Verifications() {
            Matchers.allOf(Matchers.<Person>hasProperty("name", Matchers.equalTo(name1)),
                Matchers.hasProperty("age", Matchers.equalTo(age1)))));
        times = 1;
            Matchers.allOf(Matchers.<Person>hasProperty("name", Matchers.equalTo(name2)),
                Matchers.hasProperty("age", Matchers.equalTo(age2)))));
        times = 1;

The withArgThat() method can take any of the Hamcrest matcher of type <? super T> and returns an object of type T. This is where the magics happen because you can create a matcher that matches anything you need. In addition to the many convenient factory methods provided by Hamcrest Matchers util class, you can always provide your own matchers by implementing the Matcherinterface or extending any of its implementing classes. Here you can find a few good tutorials. Also you can refer to this one.

Posted in Java | Tagged , , , , | Leave a comment

Spring Bean Configure XML Accessing Context.xml JNDI Resources

Tomcat context.xml can expose environment variables accessible by Spring bean configuration xml files.


Sometimes you want to externalize some application properties, such as password or QA/Production specific settings, used by Spring beans. There are a few options for doing so and this post focuses mainly on Tomcat based web application by putting the properties in its context.xml file.


Step#1: Put the properties into context.xml as environment entities

  <Environment name="myPassword" value="xxx" type="java.lang.String" override="false"/>

This essentially exposes the properties to JNDI java.comp/env context.

Step#2: Access them from Spring bean config file

<beans xmlns=""
<bean id="myPasswordBean" class="org.springframework.jndi.JndiObjectFactoryBean">
	<property name="jndiName" value="java:comp/env/myPassword"/>

Now you get a reference of myPassword defined in the context.xml.

Other Solutions:
There are a few other ways to externalize the bean properties without Tomcat, such as passing them in from JVM arguments or in a separated properties files using PropertyPlaceholderConfigurer. They are not covered by this post.

Posted in Java | Tagged , , , , , , | Leave a comment

Use SpringBoot to Deploy ActiveMQ Broker to Tomcat-2/3

This post is to continue the first part of the same topic. It mainly covers:

  1. Use commercial database as the data store
  2. Use the embedded Tomcat to manage the data source connection pool


Despite the fact that the ActiveMQ buildin Kaha database is more efficient when handling messages, we sometimes need to take other consideration too. For example, if your production environment prefers to database based backup and disaster recovery than NAS, you have to consider database.

Use MS-SQL-Server as the data store

There are many ways to create a datasource for ActiveMQ. This post creates one with SpringBoot and pass it to Tomcat for pool management.

Step#1: Add dependency spring-boot-starter-jdbc into the build file, and then add the following section into the file

Step#2: Disable broker auto-start. You need to ensure the broker is started after the data source is injected. This is done by calling


in the SpringBoot main() method, and then explicitly start it after setting the persistent adapter for the broker:


Use Tomcat to manage the connection pool

This can be easily done by adding the following into file

#Tomcat connection pool
spring.datasource.tomcat.validation_query=SELECT 1

Source Code

The source code can be viewed or downloaded from here.

Special Notes

You may see build error due to missing mssql jdbc driver. You need to specify your repository that hosts the driver artifact, or put it into your local gradle cache.

You can find the next related post here.

Posted in Java | Tagged , , , , | 1 Comment

Use SpringBoot to Deploy ActiveMQ Broker to Tomcat 1/3

This topic has three parts. Also See part2 in next post and part3.

ActiveMQ can be installed as a standalone server, or it can run within other containers, such as Spring or web containers. When deploying in Tomcat, you have the options of deploying a war file to a standalone Tomcat or using the embedded container provided by SpringBoot.

VM Based Broker in SpringBoot

If a VM based broker is good enough for you (when prototyping or integration testing), you can easily get it up running in minutes. Please see the ActiveMQ SpringBoot example as your start point.

Fully Configurable Broker

If you need a broker with full controllability, you can follow this post here. This post uses Gradle for build. Please see the source code in GitHub. Or you can download the commit by clicking here.

  1. Dependency.  In this example, we selectively choose a few basic individual activemq artifacts instead of using the SpringBoot ActiveMQ starter. Note that there is a activemq-all artifact available, but you may end up in logback conflict with SpringBoot. Since activemq-all artifact is generated by Maven shade plugin, it is hard to exclude logback from activemq-all. So this post uses individual jars. If you need to support other functionalities, please reference to activemq-all pom file.
  2. ActiveMQ configuration is provided by activemq.xml. Here you have all the flexibility to configure your broker. Alternative to the XML based configuration, you can in fact create a BrokerService bean and configure it programmatically in Java.
  3. The ActiveMQ broker is managed by Spring context defined in my-beans.xml, which is exposed to the SpringBoot by annotation @ImportResource(“classpath:my-beans.xml”) in the class.
  4. provides variables that you can change value without changing code. You can also pass in different values when you start your program.

Build and Run

Assume you already have gradle installed, then

  1. git clone
  2. change your working directory into activemq-server
  3. gradle build
  4. find the executable jar file from activemq-server/build/libs/activemq-server-1.0.jar
  5. java -Dserver.port=9988 -jar activemq-server-1.0.jar

Now you have a running activemq broker listening tcp port 61616.

Posted in Java | Tagged , , , , | 2 Comments

Use JIRA as Your Workflow Engine

The main advantage of JIRA, by comparing to the most of other Java Workflow engines, is that it is an issue centric system. It does not only provide workflow management for issues, but also provides other issue oriented functionalities such as reporting, searching, tracking, authorizing, assigning and timing etc. A thorough discussion can not be covered by blogs. Here I plan use a few blogs to highlight (or provide some links) a few things when customizing JIRA as part of your software application.

JIRA Product

JIRA has three different flavours, JIRA  Core, JIRA Software and JIRA Service Desk. At the time of writing, JIRA Core is not a standalone product and it is used as the foundation of the latter two products. JIRA Software pre-bundles software development plugins such as the agile, scrum and bug tracking functionalities. JIRA Service Desk pre-bundles some IT or support desk related functionalities. One big sale point of the JIRA Service Desk is that it integrates a customer portal, which can be used for expose some controllable and customizable information to your customers.

You can try any edition of JIRA for one month for free. Or you can pay $10 for a perpetual license.

If your product requires high scalability and reliability, you can consider the clustered data center version, which  of course costs much more.

Install and Run

JIRA can be downloaded from Atlanssian website. You can  run its installer to install it. Some of the key concepts during installation are:

  • JIRA installation directory, where you install JIRA application to
  • JIRA_HOME, where you store JIRA configuration (such as how to connect to DB, logs, scripts, attachment, and avatar etc.)
  • Install as service vs standalone: Install as service gives you better log output
  • DB: it uses the embedded H2 by default. You can overwrite it at the end of the installation. You can also change the database after installation but it requires more steps.
  • It also requires you to set up an admin account. Remember your password. Otherwise you need to reset it with a few steps involving SQL.

Once installed, you can log into your JIRA by http://localhost:portNumber.  Once logged in, you’d better to change the base url to http://yourHostName:port if you want other people to access it (Settings->System->Base URL).

Useful Plugins

You can find many useful third party plugins for your development or business purpose. You can go to Atlanssian Marketplace to find them. From your JIRA, you can go to Settings->Add-ons->Find new add-ons.

Two useful plugins I have tried are ScriptRunner for JIRA and Configuration Manager. Both vendors provides good web materials and excellent customer support. The ScriptRunner provided many build-in script for you to customize your fields and workflow. And it also provide the capability to provide more complicated Groovy script. The latter one let you easily manage most of your configuration, for example, generating system level or project level configuration snapshot and deploy the snapshot to different JIRA server instances. More on those two plugins later.

If you can not find any plugin fit in your specific needs, you can always write your plugin to access many JIRA resources. More on this topic later.

Develop your JIRA Data Model (Later)

  • JIRA data model
  • System configuration
  • Project configuration
  • Webhooks

Talk to your JIRA (Later)

  • Web UI
  • SDK
  • Plugins

Version Control Your Code (Later)

Deploy Your Release (Later)

Posted in JIRA | Tagged | Leave a comment