Archive for December, 2010

Get Better At: IntelliJ IDEA

Monday, December 13th, 2010

IntelliJ IDEA is a wonderful piece of software.

And that’s all I’m going to say; I won’t run comparisons against other popular IDEs, because that’s a subject for another day. Today, I want to talk about sharpening your skills with IDEA.

Shortcuts

We all know that shortcuts improve our productivity. Here are the ones I find most valuable:

  • CTRL-W and CTRL-SHIFT-W: Select progressively greater / lesser levels of code
  • CTRL-ALT-V: Introduce Variable. See also Introduce Field / Constant
  • type “iter” TAB: Java 5 style for loop Live Template. See sections below for more info on this
  • ALT-LEFT and ALT-RIGHT: View previous / next tab.
  • CTRL-ALT-LEFT and CTRL-ALT-RIGHT: Back / Forward Tab (in history).
  • CTRL-N and CTRL-SHIFT-N: Find Class and Find File.
  • ALT-F12: Show class outline (press again to also show Inherited members).
  • CTRL-D: Duplicate Line.
  • F2 and SHIFT-F2: Go to next / previous error (or warning).
  • ALT-INSERT: Create new file popup.
  • CTRL-SHIFT-F: Find in path.
  • CTRL-ALT-T: Show list of Live Templates.
  • ALT-ENTER: Show Intentions (see below for more info)

Live Templates

Live Templates are snippets of code you can insert in and around pieces of code. The most common one would probably be “Surround with If”, which takes the highlighted code and surrounds it with an If statement. What’s more, it formats the code correctly so you needn’t worry about tab spacing.

Another good Live Template is the for loop generator. Type “iter” and then hit TAB. IDEA intelligently selects a list of collections you might wish to iterate over:

To create you’re own templates, I would recommend you take a look at this Jetbrains blog post describing how to create a null-check template.

Quick Lists
You can create a shortcut to open a popup of commonly used items of your choosing. Within Preferences, go to Quick Lists to define a new list, and then Key Intentions to create a shortcut for that list. As an example:

Intentions
Intentions can be used to perform common actions, or solve problems with your code. Hitting ALT-ENTER over a valid piece of code will give you common actions. Doing the same over an error will give some suggestions such as:

  • Migrate Type
  • Cast expression
  • Import unknown symbol

Below is an example of both forms of intention:

Using WLST to find JMX MBeans on Weblogic Application Server 10.3

Monday, December 13th, 2010

I was recently asked to retrieve a selection of JMX MBean attribute values from Weblogic Application Server, and format the data as XML to be read by Nagios. JBoss Application Server provides a nice frontend within their administration console in which to naviate the MBean hierarchy, and find the MBean names you need.

Oracle doesn’t do this when it comes to Weblogic 10.3. Instead, you can either use educated guesswork in combination with some useful documentation. Or you can use WLST – the Weblogic Scripting Tool.

Using the Weblogic Scripting Tool
The first thing to do is find the binary. This is located here:

%BEA_HOME%/common/bin/wlst.bat <-- Windows
%BEA_HOME%/common/bin/wlst.sh <-- Unix

Start the executable, and wait until you receive a prompt. There is a list of common commands you will need:


connect('username','password','host:port')
serverRuntime()
cd('beanName')
ls()

As you can see, you will be familiar with the commands if you have ever used a unix command prompt before. Be aware that we execure the commands like functions, within brackets, with a number of arguments.

Example

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

wls:/offline> connect('weblogic','weblogic','localhost:7001')
Connecting to t3://localhost:7001 with userid weblogic ...
Successfully connected to Admin Server 'devserver' that belongs to domain 'vfdomain'.

Warning: An insecure protocol was used to connect to the
server. To ensure on-the-wire security, the SSL port or
Admin port should be used instead.

wls:/vfdomain/serverConfig> serverRuntime()
Location changed to serverRuntime tree. This is a read-only tree with ServerRuntimeMBean as the root.
For more help, use help(serverRuntime)

wls:/vfdomain/serverRuntime> ls()
dr-- ApplicationRuntimes
dr-- AsyncReplicationRuntime
dr-- ClusterRuntime
dr-- ConnectorServiceRuntime
dr-- DefaultExecuteQueueRuntime
dr-- EntityCacheCumulativeRuntime
dr-- EntityCacheCurrentStateRuntime
dr-- EntityCacheHistoricalRuntime
dr-- ExecuteQueueRuntimes
dr-- JDBCServiceRuntime
dr-- JMSRuntime
dr-- JTARuntime

snip....
-r-- ActivationTime 1267615098514
-r-- AdminServer true
-r-- AdminServerHost 127.0.0.1
-r-- AdminServerListenPort 7001
-r-- AdminServerListenPortSecure false
-r-- AdministrationPort 9002
snip....

The first list of items are child MBeans. The second list are attributes.

We can navigate to our JDBC connections like so:


wls:/vfdomain/serverRuntime> cd('JDBCServiceRuntime/myserver/JDBCDataSourceRuntimeMBeans/mydatasource')
wls:/vfdomain/serverRuntime/DBCServiceRuntime/myserver/JDBCDataSourceRuntimeMBeans/mydatasource> ls()
snip...
-r-- ConnectionDelayTime 97
-r-- ConnectionsTotalCount 144
-r-- CurrCapacity 10
-r-- CurrCapacityHighCount 11
-r-- DeploymentState 2
-r-- Enabled true
-r-- FailedReserveRequestCount 0
-r-- FailuresToReconnectCount 0
-r-- HighestNumAvailable 12
-r-- HighestNumUnavailable 0
-r-- LastTask null
-r-- LeakedConnectionCount 0
-r-- ModuleId mydatasource
-r-- Name mydatasource
-r-- NumAvailable 10
-r-- NumUnavailable 0
-r-- PrepStmtCacheAccessCount 7836
-r-- PrepStmtCacheAddCount 3808
-r-- PrepStmtCacheCurrentSize 99
snip...