Effective low-tech alternative to @formatter:off for Java

I love automatic formatting for source code. Back in 2004 I scrapped the detailed formatting guide for the project I was running and replaced it with a single sentence: all code must be formatted by the automatic formatter using the project’s conventions. Done. Sure, manual formatting can look better, but at what cost (and tedium)?

There are some places where the automatic formatter always fails, though, for example string concatenation for SQL commands. No problem, use @formatter:off and @formatter:on and there you are – until someone comes along who hasn’t enabled the tags. And disabling formatting completely can introduce tabs where there should be spaces, for example.

Fortunately there is an effective low-tech alternative that works everywhere (cross-IDE): comments! Use line comments after each line and the formatter can’t join them:


   "select something from some_table " //
   + " where some_column = ?"

It will still handle indentation.

Categories: Java

Windows 10 update issues

With Windows 10 there is no easy way to block an update. If Windows Update thinks you need it, well then there you are. Perhaps that is good, perhaps it is bad. I don’t like it. However, it turns out to be particularly nasty if an update fails!

In my case the printer driver for HP LaserJet CM1415 had a new version, but the update consistently failed. Not a big deal, the old driver worked well – or so I thought. Unfortunately the failure to install this one update caused Windows Update to ignore all others. It would keep trying with the printer driver every and keep failing. As a result none of the really important updates got through. So, if an update fails, don’t ignore it.

When that had been solved the Anniversary Update started, but consistently failed with a black screen. It took me some time to determine why. Apparently it didn’t play well with TrueCrypt (Windows Home does not support Bitlocker). Decrypting the drive for the update solved the issue.

Categories: Windows

ActiveMQ redelivery plugin fails when client side redelivery is active

We recently had serious issues with the ActiveMQ redelivery plugin. Under high load messages would not be redelivered at all (according to the application logs), they went straight to the DLQ. In isolated tests redelivery worked, but not as intended. We would get six redeliveries in a few milliseconds and then nothing. Why?

It turned out to be a conflict between client-side redelivery and broker redelivery. The client side redelivery kicked in and quickly failed six times. Then when the broker got a chance the maximum redelivery count had already been reached, so the message was moved to the DLQ. See this post.

Following the advice in the post we added jms.redeliveryPolicy.maximumRedeliveries=0 to the broker url, and voila! It worked.

Categories: Java

Prevent hawtio from phoning home

Hawtio is bundled in several applications as a management console. In one project we are using it with JBoss EAP 6. However, when the application server starts hawtio attempts to update itself from Github:


Performing a pull in git repository .hawtio/config on remote URL:
https://github.com/hawtio/hawtio-config.git.
Subsequent pull attempts will use debug logging
Failed to pull from the remote git repo with credentials null due:
https://github.com/hawtio/hawtio-config.git:
407 Proxy Authentication Required. This exception is ignored.

I certainly don’t want applications in production to update themselves dynamically with unforseen effects and neither do I want them to phone home. What to do? Fortunately it is possible to control this, see the documentation. Simply add the following Java options:


-Dhawtio.offline=true
-Dhawtio.config.cloneOnStartup=false
-Dhawtio.config.pullOnStartup=false

Problem solved.

Categories: Java

Free toolset for JMS tests released

In 1996 I worked with IBM MQ Series using OS/2 and CICS. That was the first time I worked with performance tuning for messaging. Since then I have been involved in many projects with the same basic goal: make sure it works correctly (no lost messages under any circumstances) and make it fast. It has never been my full-time job, but as a consultant I have been there and done that a few times now. The more memorable have been webMethods, AQ, HornetQ, ActiveMQ/AMQ and (again) AQ. Every time I have ended up writing my own tools. Yes, every time. So, I’ll do it again, but this time I’ll release the code and make it work for as many JMS providers as possible. Or at least the ones I need.

You can find the project, now in 1.0, at GitHub. If you find it useful, please let me know. One major benefit of making the code open source is that the vendors always need to reproduce any issues on their own. With an open source tool that is easier. A widely used tool is even better, as it has some credibility from the start, so spread the word (and the code)!

If I have time during the summer I will write a series of posts on how to use the tools. Let me know if there is anything in particular I should cover!

Categories: Java, Performance

Debug XA transactions in JBoss EAP 6

How do you find out if XA transactions are actually working and if there are problems, how do you find them? Apart from good tests the answer is to read the logs. So, how do you enable detailed logs for XA transactions in JBoss EAP 6? Fire up the cli (jbossctl.sh cli) and run:


/subsystem=logging/logger=com.arjuna.ats.jta:add(level=TRACE)
/subsystem=logging/file-handler=ARJUNA:add(file=
 {"path"=>"arjuna.log",
  "relative-to"=>"jboss.server.log.dir"})
/subsystem=logging/file-handler=ARJUNA:read-resource
/subsystem=logging/logger=com.arjuna.ats.jta:assign-handler(name="ARJUNA")

This will log most of the relevant XA-related events to a new log file, arjuna.log. The noise ratio is very high, so the file will grow quickly. Be sure to disable logging after a while:


/subsystem=logging/logger=com.arjuna.ats.jta:write-attribute(
  name="level", value="WARN")

Sometimes it may be necessary to log even more. These are recommended by Red Hat:


/subsystem=logging/logger=org.jboss.jca:add(level=TRACE)
/subsystem=logging/logger=org.jboss.as.connector:add(level=TRACE)

Obviously that will make the log files grow even faster, so take care! Don’t try this on a busy system in production.

Categories: Java

JSF view scope with multiple tabs using JavaScript and sessionStorage

JSF applications often use view scope for server-side state. While that is convenient it can be problematic to handle multiple browser tabs. The backend has no way of knowing which tab the user is working with. The session cookie is global for the entire browser and any URL
parameters or hidden parameters will be copied to new tabs. What to do? And even worse, what to do when you have to live with an ancient version of JSF (2.0 in our case)?

It is fairly easy to write a custom view scope on the server side, been there done that a few times. In order to pick the right beans it needs to identify the active tab. The browser will not help us, so as usual these days we must resort to JavaScript.

My first idea was to use the sessionStorage attribute. Add a script to the top of every page that looks for a window id in the sessionStorage. If it is found, add a hidden field with it to all forms in the loaded page, register a click handler that prevents navigation from links and sets the location manually with the id appended as a query parameter, override window.open in order to append the id as a query parameter in that case as well and finally register a ajaxPrefilter handler with jQuery in order to intercept AJAX requests and add the id as a request header. Oh, and rewrite the history in order to remove the id from the browser’s address line as well.

Combined with a servlet filter that denies requests that lack the window id by returning a short JavaScript function that sets it and retries this worked well – in Chrome. Internet Explorer was not as helpful. According to the specification:

When a new top-level browsing context is created by cloning an existing browsing context, the new browsing context must start with the same session storage areas as the original, but the two sets must from that point on be considered separate, not affecting each other in any way.

The major browsers diverge in how they interpret that. To cut things short my cunning plan failed.

Fortunately there is an attribute that is unique across browser tabs: window.name! I changed the code to store the unique id in window.name instead and voila! It worked.

Lesson learned – as usual the browsers can be trusted not to be consistent. Take care with sessionStorage.

Categories: Java