Archive for the ‘java’ Category

Post on Captain Holly Java Blog about DBVisualizer

Thursday, March 25th, 2010

I wrote a careful review of DBVisualizer at my java blog.

new java blog

Thursday, June 4th, 2009

Here is Captain Holly’s Java Blog. It will cover things that bore the living crap out of many of the readers of this blog. It is not focused on the latest java news. Instead it focuses on how stuff works. It covers:

  1. Java
  2. other java related languages
  3. Competitors to Java
  4. design of computer languages, virtual machines, and compilers
  5. Web related stuff
  6. Security software and how it works
  7. Information display and reporting

So far, I’ve used it to review code from open source projects to see how it works.

It is named after Captain Holly of the Saddleford Owsla.

Tomcat Seminar

Thursday, May 14th, 2009

I attended Wednesday’s SpringSource Tomcat Seminar. Spring Source has a product based on Tomcat (Spring tc server). As a way of promoting this, they put on a brilliant seminar featuring Filip Hanik about deployment, performance and troubleshooting of Tomcat. The promotion of TC Server took about 20 minutes of a 4 hour seminar. The format was well thought out: “Here are are some really interesting ways Tomcat can be used, here are the command line tools to do it by hand, and finally, here is our product that helps automate and organize some of these things.”

Information came at such a rapid pace that I barely had time to write down keywords. He did NOT simply read from the slides like a dolt. Here are the slides.

  • setenv isn’t documented, but when Tomcat starts, it looks for a file called setenv.sh|bat for instructions about which jvm to use and which Tomcat version to use. A default Tomcat install has no setenv.sh|bat and so defaults are used. For example the default JAVA_HOME is the java home in your system environment variables. Well, shit. All these years I’ve been changing my system’s JAVA_HOME when I could have been fixing setenv.
  • supports variables like {port} in configuration files. This means you can have one server.xml file with a variable parameter for port, and the port is defined by which Tomcat instance you feel like starting that day. This is a huge advantage in staging and testing environments as you could be testing with multiple Tomcat versions and java versions.
  • Hanik voiced skepticism about the wisdom of hot redeployment in production: failure to explode war files fast enough, memory problems, and problems with serialized sessions were all offered as reasons for this.
  • Performance
    1. Logging Affects performance. Logging too much can eat up memory. For production environments, one should disable duplicate loggers.
    2. maxThreads. If CPU useage is low, Tomcat is not taking advantage of hardware and you should increase thread count. By the same argument, if CPU usage is high, lower maxThreads.
    3. maxKeepAlives. KeepAlives are simply keeping TCP connections alive so the handshake does not have to happen again. This setting governs how many requests are allowed from one TCP session. This is especially important if using SSL since handshakes are more costly there.
    4. ConnectionTimeout. How long without activity before a connection is terminated.
    5. AcceptCount: This is a setting that in a way caches a bunch of TCP/IP handshakes. There is also an Operating system level setting for this same activity.
    6. For the most part, tuning Tomcat itself is limited. Tweaking Tomcat won’t do much good if your application is not tuned. The JVM and Operating System need some attention too.
    7. Send and Recv buffers, set at Operating System level, are key to performance tuning.
    8. Garbage Collection affects performance. Heh. That sentence hides the existence of the field of Garbage Collection-ology. He sped through ways to debug garbage collection
  • Troubleshooting
    1. We already know that the errors that end up in Catalina.out are those that are not caught by our applications. But, we can configure the way that errors are reported by Tomcat. For example, outOfMemoryException could be any one of 15 errors, but by default Tomcat will just report outOfMemoryException. With a simple command in the startup options for Tomcat, we can tell Tomcat to sift through the error types and take different actions depending on the errors. For example, make a heap dump when
    2. Heap Dumps and other info can be had with Java’s VM arguments such as XX:-HeapDumpOnOutOfMemoryError. Many more are here: http://java.sun.com/javase/technologies/hotspot/vmoptions.jsp
    3. Thread Dumps are very informative. They can show deadlocks. Use kill -3 or Jstack. Java 1.6 has jstack built in.
    4. Profilers: The act of observation changes the thing you’re observing. Profilers will affect your code execution. To minimize the change in outcomes while you do detective work, it is advised to wait until you narrow down the problem before bringing profilers in.

Type Erasure

Wednesday, April 15th, 2009

Studying for Java certification, I’m still working through Niko Java’s fake exams. Today is Generics.

I just caught a clue about type erasure and I want to document:

The secret to generic types is that they are only for the compiler to look at. In the final compiled product, they have been stripped away. This is called type erasure. The compiler just checks that the program, when it finally runs, cannot violate the agreement entered into with generic type declarations.

This affects method over-riding in the following way:
if the method in the Child has a generic type and the parent does not or the parent has a different generic type, the compiler will see these as two different method signatures and not as a case of over-riding, so over-riding fails. But the compiler also knows that in the end product, the generic type definitions will be stripped away, leaving two methods with the exact same signature, but with no instructions of which one to call. This will result in the compiler error theMethod in parent and theMethod in child “have the same erasure, yet neither overrides the other”

If the Parent has the generic type while the Child does not, then method override succeeds and the program will compile with warnings about type safety. These warnings mean that as a result of a mixture of generic types and non-generic types, the compiler cannot guarantee that at runtime, there will be an unexpected type thrown in somewhere.

nikojava

Friday, April 3rd, 2009

let me introduce you to niko’s java blog. Niko invents long, repetitive java mock exams. The repetition is an excellent way to learn and remember all the things you’ll need for the exam. I find that by answering each question and giving a reason for my answer gives me better understanding. It’s taken me a week to get through just his collections exam, but damn do I know collections now. And I THOUGHT I knew them damn well before. Never let anyone tell you that the Java Certs are worthless. Studying for this exam is filling in lots of gaps and putting important concepts at my fingertips instead of a web search away or an hour of trial and error away.

Pulse! Borked Today, but still cool.

Thursday, September 25th, 2008

Pulse is an Eclipse package installer. It not only manages mirrors and dependencies for eclipse and its packages, but it allows you to save (and share) profiles. The potential for this includes setting up a profile intended for the developers at your company so they don’t have to mess about installing their needed eclipse plugins by hand, making it easier for you to migrate your eclipse install between systems.

Last week, the installation was very slick indeed and I thought a new era of ease had arrived. Unfortunately, their servers (pulse or eclipse project) are moving verrrrrrry slowly today. I hope they nip that in the bud, toot sweet!

Lambda Probe

Friday, August 29th, 2008

Always eager for any clue of what Tomcat thinks it is doing, I downloaded Lambda Probe It was built to monitor Tomcat performance and it is a beautiful and easy-to-use tool. It was last updated in 2006, a lifetime ago, but judging by the forum activity, it still enjoys wide use. It installs as a war and provides a nice web interface that shows, among other things, thread information, database connection usage, some limited visitor stats, charts for traffic volume, access to log files and log tails and much more.

It is very easy to install.
One thing that pleased me immediately: It shows insight into individual application settings such as session timeout and even the list of jar files that the classloader has loaded. This is so much nicer than opening up some properties file some place to check settings.

And Context attributes. and, good god… a drillable list of every session and the session attributes and values within. I’m in tears.

It doesn’t seem to have backend storage to allow you to look back over time at performance as this unanswered question from the forums hints at:

Is there a way to run a report so I would not need to rely on monitoring the system in real time?

Good question.

SQuirrel SQL Client

Thursday, August 25th, 2005

I have been testing out SQuirrel SQL Client today. SQuirrel first came out a few years ago and it has recently been resurrected. Version 2.0 officially came out Aug. 23. It is built with Java Swing and so is supposedly cross-platform. It is open source and it supports every database imaginable. I tested it out with MySQL. I seems to have the features you’d expect in a desktop SQL client such as spreadsheet-like editing and SQL syntax highlighting.
Notes on features and interface.

  1. Simple but nice autocomplete feature that takes the approach of loading every possible thing you might type into one big pop up. List shrinks as you type. In many cases, it loads the things you are most likely to type at the top of the pop up list.
  2. Shows a breakdown of the time it took both to run the query and display it.
  3. Data grid has a good copy to HTML feature.
  4. Plugin architecture
  5. Default plugin included to change Look and Feel
  6. One minor annoyance I noticed in the interface is that popups from the editor screen do not disappear when you leave that screen.
  7. Interface is very responsive. I couldn’t get it to hang in the way that poorly written swing applications sometimes hang.

Here is a squirrel screenshot:

Leaving the cozy IntelliJ womb

Thursday, August 18th, 2005

IntelliJ IDEA can make you feel like a genius. Using IntelliJ, you can write java code fast and it works the first time. It has a nice friendly environment that can lull you to a sense of well-being. Being a java programmer, you know that is a false sense of well-being and this will become clear when you try to deploy the app you’ve built outside the forgiving environment of intelliJ. One obstacle is to make sure that the IntelliJ IDEA libraries that enable the attractive GUI layout to follow the project outside of IDEA.
Here is a short list of steps to deploy a webstarted application built in intelliJ:
Too keep things simple, I am just using an application with one class, LakeSurveyTest that gets wrapped into one jar (LakeSurveyTest.jar) with the required intelliJ libraries.

  1. Notice in the ANT file that it grabbed some IntelliJ specific .class files and put them in the .jar file. This is only needed if you used their GUI builder.
  2. For some reason, this worked fine when I told IDEA to put the GUI stuff in the Java code instead of in binary class files. (I think my ant build failed to copy over the binary class files )
  3. I also need to sign my .jar so that my application has the privileges it needs on the client’s machine. ANT lets us do this with the genkey and signjar tasks.
  4. We need a .jar file to hold these class files. Normally, this is done with ANT. Here is a sample ANT build file for getting that done:
    [code lang=”xml”]














    destdir = "${classes.dir}"
    />















    [/code]
  5. Set the classpath for running the application out of the .jar file. IntelliJ has taken care of this for you based on what you said when you went to settings/paths/classpath(libraries). Now you have to do it yourself.
  6. Once you have a jar file for deployment, test it on your own machine by setting the classpath to your jar and then setting the classpaths to any libraries you might have bundled up inside your jar file.
  7. Now put together a .jnlp descriptor file like this one:
    [code lang=”xml”]


    spec="1.0+"
    codebase="http://www.fake.com/Survey"
    href="LakeSurveyTest.jnlp">

    Tim

    Test Application

    Web Started Test App










    [/code]

  8. tweak .htaccess to recognize the .jnlp filetype and put your jar file and .jnlp on a server somewhere. Point your browser at the .jnlp file and watch the show.

code kata

Tuesday, August 9th, 2005

Uncle Bob’s PrimeFactors kata is a great way to learn about the new features of JRE 1.5 (static imports, varargs, generics) as well as some simple JUnit stuff.

A kata is an exercise in martial arts that you repeat a form over and over again with variations to learn a certain technique.

JFormattedTextField fixes my dates

Tuesday, July 12th, 2005

If you use a JFormattedTextField in Swing to accept user entered dates, you may be annoyed by the default behavior of correcting dates. If the user enters “March 36th”, it will automatically “fix” it to read “April 5th”. Clever little text field! You deserve a pat on the head for figuring that out, now DON’T EVER DO THAT AGAIN!. I saw this question asked on Javaranch.com and the solution is posted there. I was searching around for a way to do this with the JFormattedTextField, but it turns out you have to set the behavior of the DateFormat object to not lenient:
[code lang=”java”]

DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
df.setLenient(false);
final JFormattedTextField text = new JFormattedTextField(df);

[/code]

screenshot of Swing App

Friday, June 3rd, 2005

Here is a first shot (O.K. - fifth shot ) of a particular screen for the lake survey application. If anyone cares to, I would love some feedback. It is kind of the template for all the parent-child editing screens in this application.

keyboard shortcuts

Wednesday, June 1st, 2005

Here’s five keyboard shortcuts that have been saving me lots of time in IntelliJ today:

  • CTRL-n : Navigate to a class by typing the first few letters of its name. This lets you close the project tree and free up some screen real estate.
  • CTRL-Shift - backspace : go to last edit location
  • CTRL-ALT-Left : undo the last navigation performed
  • CTRL-ALT-Right: redo the last navigation
  • CTRL-B : navigate to the declaration of the method the cursor is on

I have to post stuff like this so I can remember it later. I try to mark all computer posts so that if a visitor browses to the “general” category, they will miss them. The ultimate solution is to make this configurable, so visitors can select the categories they are interested in and the web site will remember their preferences. hmmmm….

bizarre swing behavior

Thursday, May 26th, 2005

Ahhh Swing. If I have a JList and put a ListSelectionListener on it, the listener will fire twice, once for the item you select and once for the item you de-select. Except if you downclick and move the mouse around without an upclick, it only fires once. How to tell it to just fire once for the item I selected?

Answer: It is going to be fired twice. To make sure the event code only runs for the newly selected value, use if(event.getValueIsAdjusting()){....} to make sure the JList is done thinking. No, I am not joking.

BTW, with a JList, you can use setSelectionMode() to allow just one selection, multiple selections, or multiple non-continuous selections with a line like this:
manage.lakeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

hibernate behavior

Wednesday, May 25th, 2005

It says in the Hibernate Docs that I can use the MAX ( ) function in a hibernate query just like I would in a regular SQL query. In practice, though, it returns more than one object as if I didn’t have the MAX ( ) in there at all.

A thing about Swing

Sunday, March 27th, 2005

The default behavior for rendering an object in a JComboBox is to call the toString() method of that object. If you don’t have a toString() defined, the JComboBox shows an object ID. So, the first try at using a JComboBox is to implement the toString() method in all your listable objects.

When in a development environment where you have to use other people’s objects, they might have other uses for toString() and that might not be available to you. So, the next try is to use custom renderers for all your listable objects. This is nice because it gives you complete control over how something is rendered in a JComboBox. A custom renderer returns a Component, usually a JLabel, though it could be made to return checkboxes and other components. One surprise is that once you start using a Renderer, all the usual behavior of a JComboBox (a blue box around the item you want, for example ), disappears. The first time you try it, you might find that the item you selected from your JComboBox becomes invisible.
There does not seem to be a reference to this, either in the Java API or in my Swing book, but setOpaque() must be set to true in your renderer for the JLabel be visible. The renderer does not assume that you would want to actually view the JLabel you requested. You also write your own behavior for what the JLabel does when you select it, meaning set the background to blue as follows:

if (isSelected) {
setBackground(Color.blue)
setForeground(Color.white);
} else {
setBackground(Color.white);
setForeground(Color.black);
}

Using custom renderers is explained here, but they don’t come out and tell you that part about setOpaque.

convert PreparedStatement to String

Wednesday, November 17th, 2004

Problem: I would like to see the text of the SQL that a prepared statement is executing. The PreparedStatement object does not override toString() and you can never see the actual SQL.

Solution: I found this javaworld article that describes how to do this. I applied the recommended fixes to their code. I then wrapped these classes into javaunderground.jar Now I can place this jar file in the lib directory of my web application, or in my IDE’s libraries and I will have the ability to turn debugging for PreparedStatements off and on.

instead of
PreparedStatement ps = conn.prepareStatement(sqlString)

I now use

PreparedStatement ps = StatementFactory.getStatement(conn, sqlString);

When debugging is turned off, the latter will return a regular PreparedStatement
When debugging is turned on, the latter will return a DebuggableStatement which has toString() overridden so that I can call ps.toString() and see my actual SQL.