Enabling HTTP request response logging on JBoss 4.2

Today I had a bit of a fiddle with JBoss 4.2 due to IE7/IE8 compatibility differences.

When opening or downloading a file in IE7 over an SSL connection (HTTPS), IE7 requires a bit of HTTP header-fu to allow you to actually perform that action.

It’s simple really. Just add “Pragma: public” and “Cache-Control: maxage=3600” to your HTTP headers and you’re good to go.

In Java code:

response.setHeader("Pragma", "public");
response.setHeader("Cache-Control", "maxage=3600");

To check if it’s working was a different matter. How to check the HTTP header content when things get sent over an HTTPS connection… FireBug doesn’t work, and so doesn’t any other developer tooling in any browser. Tracing the network traffic is pretty useless too, it’s encrypted. Last option, trace it on the server. Simply enabling network tracing on the Java VM didn’t work. For some reason JBoss doesn’t pick up this Java VM setting, probably because they are using a NIO based stack or something. I really don’t know.

JBoss does use Catalina for it’s HTTP/HTTPS/Web container handling. Catalina is another name for Tomcat. Tomcat has this valve thing which allows you to plug into the processing pipe. Good thing there are standard Valve implementations available that do just what I needed.

Just drop the next snippet somewhere appropriate:

<!-- Enable http request/response logging.-->
<Valve className=org.apache.catalina.valves.RequestDumperValve
           prefix=localhost_access_log. suffix=.log
           pattern=common directory=${jboss.server.home.dir}/log
           resolveHosts=false />

Appropriate in my case was in the file “[JBOSS-HOME]/server/default/deploy/jboss-web.deployer/server.xml”.

And lo and behold it logged:

11:46:44,262 INFO  [[localhost]]           authType=BASIC
11:46:44,262 INFO  [[localhost]]      contentLength=48189
11:46:44,262 INFO  [[localhost]]        contentType=application/pdf
11:46:44,262 INFO  [[localhost]]             header=Pragma=public
11:46:44,262 INFO  [[localhost]]             header=Cache-Control=maxage=3600
11:46:44,262 INFO  [[localhost]]             header=Expires=Thu, 01 Jan 1970 01:00:00 CET
11:46:44,262 INFO  [[localhost]]             header=X-Powered-By=Servlet 2.4; JBoss-4.2.3.GA (build: SVNTag=JBoss_4_2_3_GA date=200807181417)/JBossWeb-2.0
11:46:44,332 INFO  [[localhost]]             header=X-Powered-By=JSF/1.2
11:46:44,332 INFO  [[localhost]]             header=X-Powered-By=Servlet 2.4; JBoss-4.2.3.GA (build: SVNTag=JBoss_4_2_3_GA date=200807181417)/JBossWeb-2.0
11:46:44,332 INFO  [[localhost]]             header=X-Powered-By=JSF/1.2
11:46:44,332 INFO  [[localhost]]             header=Content-Type=application/pdf
11:46:44,332 INFO  [[localhost]]             header=Content-Length=48189
11:46:44,332 INFO  [[localhost]]             header=Date=Thu, 26 Aug 2010 09:46:44 GMT
11:46:44,332 INFO  [[localhost]]            message=null
11:46:44,332 INFO  [[localhost]]         remoteUser=admin
11:46:44,332 INFO  [[localhost]]             status=200

-XX:-DontCompileHugeMethods

Read some stuff today about huge methods and the JVM’s JIT Compiler.

The JIT proces detects hotspots in code. If a hotspot is detected (usually 1000 to 1500 hits depending on your platform and JVM version) the JIT process marks a method for compilation to machine instructions. After compilation, method invocations are generally crazy fast on a method since the JIT process had like a thousand times to see what generally happens during the execution of a method and can optimise accordingly. (So never ever use the Xcomp option on the JVM, it forces compilation from the start, but this is generally slower, because the compilation will not be optimized in any way.)

The thing is, when performing this operation of marking methods for compilation there’s a catch. An 8k byte code instructions catch to be exact. If a method in byte code contains more then 8000 byte code instructions it will never ever be marked for compilation, period. About 2500 lines of Java code should do the trick. But believe me, I’ve seen crazy fools create monsters methods of that magnitude.

It gets even better though. When looking for methods to mark for compilation, the JIT process uses a call graph when doing it’s magic work of granting methods the privilege of optimized compilation. And here comes the catch in that, if a big fat honking pile of 8k byte code is encountered in a method while walking the call graph, JIT says: “I aint touching that!”. Thus any method called from that pile of poop not reachable in any other way through the call graph, will never ever feel the warm love of JIT optimized compilation.

Now you could use the lovely “-XX:-DontCompileHugeMethods” argument when invoking the JVM. But from what I’ve understand it’s a bad thing to do, because amongst others it forces the JVM to go digging through those 8k wonders of human creativity.

Other option is to just use your sane mind and round up and terminate the morons in your project creating these 8k monsters. It’s good practice anyway to keep your methods small and task focussed in any object oriented language.

The gist is, don’t use “-XX:-DontCompileHugeMethods”. Go bananas during a code review whenever you find any method which is too big for comfort. Let’s just say that the default statement count suggested by checkstyle is a good one. So any method or constructor above 30 statements in length should be cause for investigation during a code review.

JDK 7 – Project Coin is final

Friday Joseph Darcy announced through a blog post that Project Coin is final.

Project Coin is just one of the changes coming to JDK 7. But, since the changes in Project Coin are all small changes to the Java language, every single Java developer will have to deal with these coming changes.

  • It’s looking like we can use Strings in switch statements:
    static boolean booleanFromString(String s) {  
      switch(s) {  
        case "true":  
          return true;  
        case "false":  
          return false;  
      }  
      throw new IllegalArgumentException(s);  
    }  
  • Automated Resource Blocks, Josh Bloch proposed a construct like the one below:
    try (BufferedReader br = new BufferedReader(new FileReader(path)) {
      return br.readLine();
    }

    instead of:

     BufferedReader br = new BufferedReader(new FileReader(path));
     try {
       return br.readLine();
     } finally {
       br.close();
     }
  • Improved Type Inference, the diamond operator. Instead of:

    Map <String, List <String> anagrams = new HashMap <String, List <String>();

    you can do:

    Map <String, List <String> anagrams = new HashMap <>();
  • Simplified VARARG method invocation:
    static <T> List <T> asList(T... elements) { ... }
    
    static List <Callable <String> stringFactories() {
      Callable <String> a, b, c;
      ...
      *// Warning: ** uses unchecked or unsafe operations *
      return asList(a, b, c);
    }

    After this change:

    *// Warning: ** enables unsafe generic array creation *
    static <T> List <T> asList(T... elements) { ... }
    
    static List <Callable <String> stringFactories() {
      Callable <String> a, b, c;
      ...
      return asList(a, b, c);
    }
  • Something with better literal integers, can’t seem to find the details at the moment.
  • And language support for JSR 292. But this one is only interesting when dealing with dynamic languages.

JBoss with SQL Server and EJB3 is giving me a hard time

Today I had a bit of a fuss with JBoss and SQL Server. For some reason, when I declare an entity with an Id field, the persistency layer tries to push a bad query to the database.

Let me explain.

I’ve got an entity sort of looking like this:

@Entity
public class Contract {

@Id
@GeneratedValue(strategy = GenerationType.Identity)
private int contractId;
...
}

When tryin to persist this through the entity manager SQL trace on MS SQL show a query somewhat looking like:

insert into Contract (contractid, ...) values (null, ...)

You see, a null value. Sounds reasonable, I didn’t set any value on the contractid field. BUT the persistency layer should not try to insert anything into the contractid column, because MSSQL doesn’t allow it. The above SQL results in a “DEFAULT or NULL are not allowed as explicit identity values.” I’m not sure what to try next though, everywhere I looked on the internet, all examples boil down too: “Something similar should work in MS SQL too, but I haven’t tested that…”

Better luck tomorrow. For now, I googled my brains out trying to find anything helpfull.

Anybody out there have any suggestions?

Update on the next day:
It turns out that the problem was the dialect configured in the Hibernate properties in the persistence.xml. By default our development environment enters the right value for Hypersonic DB, while it should actually be like this:

<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>

And now it all works. A default SQL Server 2005 with JBoss and the latest MSSQL JDBC driver, all running EJB3.

Back from the JavaOne 2009

Phew, back from the JavaOne 2009 again.

Too bad Jonathan's assistent didn't allow my camera to focus. First time I’ve been there. And boy could you feel the recession at the JavaOne… I think the number of visitors was at an all time low. Also in San Francisco itself recession was around you. Loads and loads of properties available for lease or sale.

But me and my colleagues did our best to support the global and local economy by living large. 🙂 Besides the conference we did all sorts of fun activities. I even biked the Golden Gate the last Friday afternoon.

I also learned a valuable lesson when returning from another time-zone. When I got home I did some things and then went to my girl’s place. I should’ve asked her to keep me awake at all costs. Sunday I sat down on her couch and I kind of instantly fell asleep. I paid the price on Monday and Tuesday. Fortunately my employer was very understanding about Monday and Tuesday I’ll recover by working a few extra hours this week or next week. 🙂

More to come later. Suffice to say that on record nobody from Sun was allowed to spill any details on the Oracle/Sun merger. I understood of the record that there will be another JavaOne next year. Let’s hope that by then it is clear what will happen with Java, cause right now it’s anybodies guess what Oracle has in store for us. I’m expecting a whole lot, but it is still a bit early to say what exactly Oracle will keep and what Oracle will throw away. I do think JavaFX is here to stay though, the new JavaFX 1.2 is a major improvement compared to the 1.1 release. Check Paul Bakker’s entry on JavaFX for more details.

Too bad the assistant of Jonathan Swartz didn’t allow my camera to focus properly.

Also many thanks to Bert, Paul, Marcel and Hans for being such great travel companions throughout the week.

Yup, this time it seems to be really it for SUN

It seems Oracle has put forward a bid on SUN Microsystems. Sun apparently has accepted this offer and both parties are now in final negotiations.

Personally I didn’t see this one coming. The IBM/Sun brush a short while ago was kind of obvious to me. Still a bit of a surprise, but yeah IBM absorbing SUN I can dig that.

Now Oracle has stepped up to the plate and dumped a load of money on the table. Oracle never has been my favourite when it came to Java related technology. Lots of cool demo’s that are hard to reproduce in real life situations if you ask me. Also Oracle doesn’t give me that welcoming feeling when getting into contact with them. I guess we’ll just have to wait and see how this ordeal is going to affect Java in general.

I see a lot of competition between IBM and Oracle in the future. Because let’s just face it, is there any other big Java player to speak of if this deal gets finalised? There just aren’t any other vendors that could offer a complete no holds barred JEE stack besides those two big names.

J-Spring: That’s it for today

I’ve decided to skip the last round of sessions, it’s sunny today and air in the session rooms is very warm and stale. Also the battery if my phone is almost dead. 🙂

Update:
I’ve updated all the posts of today, checked spelling, adjusted tags and uploaded photo’s. Enjoy….

J-Spring: Brainfuck’s quest for CafeBabe – a journey through JVM land

Jeroen Benckhuijsen has this oddly called session about implementing a certain language on top of the Java VM. He’s telling a bit about implementing a custom language using pure byte code.

He’s using an interesting approach. Weird slides with a funny story.

It’s a technical talk about the how class files are laid out and how they work with the Java memory model. It’s a lot info available on the web. So I’ll shut up now. I’ll post links later.

IMG_0081.JPG

J-Spring: SOA, it’s a hard knock life

Tijs Rademakers has a presentation on SOAs in practice. What problems do you encounter when implementing a SOA. After having discussed the advantages of SOAs he dives into the rumours that SOA is dead. But hey, services are the future. Anne Thomas Manes has a contradicting statement.

It is true that SOA has not proven to reduce costs. SOA projects take longer to implement because the services have to be implemented in a re-usable way. Often a SOA project boils down to an ESB selection, but is that deciding on what SOA architecture you want to implement? Discussions often focus on the implementing technology and not the underlying SOA architecture.

Tijs introduces a case study. A COBOL applications of 14500 function points. It has been transferred to a SOA based architecture. A lot of companies belief that implementing everything as a web service is the same as implementing a service architecture. But SOA is more then that. While a bottom up approach might work, a top down view is important as well. Implementing reusable services is only possible if you take the surrounding architectural landscape into account. A canonical data model is important to facilitate a standardised data language for all services.

SOA architectures are built according to a plan, but the legacy application being replaced is also often built based on a clear plan. Code is reused is built according to an architecture of days gone by.

Is the entire web of systems and services defined within a SOA application really that much simpler then a legacy system?

Is a SOA something you should do? And if so, what technologies should you use? Is an ESB a requirement or any of the other standards. (SOAP,WSDL,WS-,SCA,OSGi,etc.)

Implementing a service is easy. Just add @WebService. A top down and bottom up is available. The same goes for REST services with Jax-RS.

Java – XML binding can be challenging. You often need to write some custom code. This is due to JAXB v1 API. But there are better frameworks available. JAXB2 JIBD XSTREAM. New languages show the way to the future. Some aspects should be ported to Java.

It’s a bit of a strange presentation. First it’s all SOA, now we’re talking Groovy??

SCA is a good option, with it’s declarative configuration and flexibility in choosing the actual implementing technology. I should read up on SCA a bit.

OSGi is also a very impressive platform. A lot of implementations using OSGi are emerging. It supports all the service oriented functionality. Wso2’s Carbon is a nice example.

Next topic, does SOA entail the usage of an ESB? An ESB is a platform you can use to implement a SOA. but it’s just a platform with a high risk of vendor lock-in, under the hood it’s all custom proprietary. You always need custom code to integrate an ESB. Vendors push ESBs as the way to implement a SOA, but it is often nothing more then a tool to create a nasty lock-in. An ESB does a lot, use it, but be careful for the lock-in trap.

An ESB often only hides a lot of shit on an overview architecture slide.

An alternative would be a sort of guerilla SOA. Unlock lots of systems with restful services. Then don’t define a lot of rules and let it grow. All you need at a certain point you need a good way to resolve the locations of services.

In the end you need business level support to be able to do a service oriented approach. Also business has to be made aware of the initial impact of a service approach. But also of the long term benefits. Consolidation of services and systems is recommended. But never forget that business often steers on short term goals while they should look at long term goals.

IMG_0080.JPG

J-Spring: half time keynote Mod4J

Jos Warmer has the midday keynote. He has an introductory talk on the Mod4J project.

Jos is an MDA enthusiast and as always makes a case for the model driven approach. Check the Internet for more details. I’ll post some links and info later, now I’ll just sit back a bit and relax a bit before jumping into the remaining sessions.
IMG_0078.JPG