2010/11/22

Maven - Generating Code with JAXB Plugin

We wanted to generate sources from multiple scattered XSDs. These schema definition files described classes in different packages with the same prefix. The setting for such configuration is not so straightforward  so I put together this small working example.

Possible sources of problems:
  1. Plugin uses lock file to determine when schema files are newer than generated files. Using the the same or default lock file name causes only first xjc plugin execution is run.
  2. By default the destination directory is cleared. With different XSDs targeting the sub-directories of the same directory you likely get that later executions deleting previously generated sources.

<build>
  <plugins>
    <plugin>

      <groupId>org.codehaus.mojo</groupId>
      <artifactId>jaxb2-maven-plugin</artifactId>

      <configuration>
        <quiet>true</quiet>
        <verbose>false</verbose>
        <clearOutputDir>false</clearOutputDir>
        <readOnly>true</readOnly>
        <arguments>-mark-generated</arguments>
      </configuration>

      <executions>

        <execution>
          <id>snmp</id>
          <goals><goal>xjc</goal></goals>
          <configuration>
            <packageName>org.bithill.testing..config</packageName>
            <schemaDirectory>${schema.dir}/config</schemaDirectory>
            <schemaFiles>config.xsd</schemaFiles>
            <staleFile>${build.directory}/generated-sources/.jaxb-staleFlag-config</staleFile>
          </configuration>
        </execution>

        <execution>
          <id>app</id>
          <goals><goal>xjc</goal></goals>
          <configuration>
            <packageName>org.bithill.testing.app</packageName>
            <schemaDirectory>${schema.dir}/app</schemaDirectory>
            <schemaFiles>app.xsd</schemaFiles>
            <staleFile>${project.build.directory}/generated-sources/.jaxb-staleFlag-app</staleFile>
          </configuration>
        </execution>

      </executions>
    </plugin>
  </plugins>
</build>

2010/06/01

Intellij Idea - Searching in Single Library with Scopes

Do you have a package in your dependencies and need to seach for some string in just this package, excluding others ?

You can do it using not so known scopes. If you do not know them, you should try, it's one of the most practical things Idea includes. It can be used e.g. for coloring your tabs but it's also more than useful for searching.

1/ Open Find in Path dialog, either from menu or via Ctrl+Shift+F shortcut 

 2/ Select Scopes - Custom.
 3/ For creating of a new scope, click on three dot buton on the right side of the scopes list.
 4/ in Scopes dialog, use the Add Scope icon (yellow plus in topl left corner). In the right panel select packages belonging to the library in which you want to search and include them recursively and press Apply.
 5/ Giving OK you'll get back to the search dialog now with your newly created scope available.

2010/04/28

Deploying WAR Locally and Remotely (via scp)

Tried to run ant scp from maven and got weird class org.apache.tools.ant.taskdefs.optional.ssh.Scp was not found, even when you have all the ant-jsch and jsch jar files in ant lib directory ? Try to add ant-jsch as plugin's dependency in maven:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
         <modelVersion>4.0.0</modelVersion>

  <groupId>org.bithill.project</groupId>
  <artifactId>project-artifact1</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Example project</name>

  <description></description>

  <dependencies>
  </dependencies>

  <build>

    <plugins>
      <plugin>

        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <configuration>
              <tasks>
                <copy
                 file="target/${artifactId}-${version}.war"
                 todir="local_deolpy_dir_name" />
                <scp
                 file="target/${artifactId}-${version}.war"
                 todir="login:password@remote_host_name:remote_deploy_dir_name" />
              </tasks>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>

        <dependencies>
          <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant-jsch</artifactId>
            <version>1.8.0</version>
            <scope>provided</scope>
          </dependency>
        </dependencies>
        
      </plugin>
    </plugins>

  </build>

</project>

2010/02/10

Apache-Tomcat Communication via mod_jk

Ever wondered why to use mod_jk and how to set it up? The reasons why are simple - Tomcat guys recommend it as more mature.

It certainly has better logging then mod_ajp_proxy and that helps alot during troubleshooting or performance tuning. The ability to set max packet size above 8kB is also a good point for some deployments. As for performance, some tests showed slight advantage of mod_jk to mod_ajp_proxy, but the main seemed to be the usage of Tomcat's native module regardless of AJP module used.

LoadModule jk_module /usr/lib/httpd/modules/mod_jk.so

# points to a file that provides a mapping between a worker name and a valid worker type
JkWorkersFile /etc/httpd/conf/worker.properties
JkLogFile /var/log/httpd/mod_jk.log

# allows log levels: debug, info, error
JkLogLevel   info
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
JkRequestLogFormat      "%w %V %T"

# JkOptions - see http://tomcat.apache.org/connectors-doc/reference/apache.html

<Location /jkstatus/>
 JkMount status
 # Order deny,allow
 # Deny from all
 # Allow from 127.0.0.1
 Allow from all
</Location>

<VirtualHost *:80>
  # which URI contexts are sent to a ASF Tomcat worker - exact, context or suffix match
  JkMount /* tomcat1
</VirtualHost>

Note: JkMount must be defined in VirtualHost to work.

worker.properites:

worker.list=tomcat1
worker.tomcat1.type=ajp13
worker.tomcat1.host=localhost
worker.tomcat1.port=8009

## when using mpm prefork, the pool_size should be set to "1"
worker.tomcat1.connection_pool_size=1
worker.tomcat1.connection_pool_timeout=600
worker.tomcat1.socket_keepalive=1

max_packet_size=65536


JkWorkerProperty directive allows to move content of the worker.properties file to apache config.

2010/01/19

JBossCache JMX in Standalone App

try 
{
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
ObjectName on = new ObjectName("jboss.cache:service=Cache");
JmxRegistrationManager jmxManager = new JmxRegistrationManager(server, cache, on);
jmxManager.registerAllMBeans();
}
catch (MalformedObjectNameException ex)
{
log.error( CommonUtils.getStackTraceString(ex) );
}

If you want to read cache statistic in the same JVM, get JMX object name of
CacheMgmtInterceptor using jconsole and use it to access its attributes:
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
ObjectName objectName = new ObjectName("jboss.cache:service=Cache,jmx-resource=CacheMgmtInterceptor");
server.getAttribute(objectName,attributeName)

Probably the most interesting attributes are named Hits, Misses, HitMissRatio, Evictions, AverageReadTime, AverageWriteTime, ReadWriteRatio and NumberOfNodes. More can be found in documentation. Note that Hits and Misses are on attribute level so if work with whole nodes, these values are useless, also when putting whole nodes into the cache the interceptor is probably bypassed.