Pokazywanie postów oznaczonych etykietą Java. Pokaż wszystkie posty
Pokazywanie postów oznaczonych etykietą Java. Pokaż wszystkie posty

poniedziałek, 1 lipca 2013

Message Driven Architecture with Spring

Here is a video introduction to MDA from Spring YouTube channel. It gives really good overview of basics including pooling, scheduling and task executing using Spring facilities. Also what is the value of this series presenter explains what are Enterprise Integration Patterns and their implementation in Spring Integration framework.



poniedziałek, 15 kwietnia 2013

Spring MVC + Apache Tiles


In this post I will show you how to configure and start coding views with Spring MVC and Apache Tiles. By default Spring don’t give us any rich user interface library and template engine, actually why it should?  It doesn’t have to.
The second one is a must have element in every web application. Spring have possibility to integrate with few template engines like Velocity, FreeMaker or Apache Tiles. I will show  you how to use the last one. Let’s do it…

1.   Configure viewResolver



       <bean id="viewResolver"
              class="org.springframework.web.servlet.view.UrlBasedViewResolver">
              <property name="viewClass">
                     <value>
                           org.springframework.web.servlet.view.tiles2.TilesView
                     </value>
              </property>
       </bean>
       <bean id="tilesConfigurer"
              class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
              <property name="definitions">
                     <list>
                           <value>/WEB-INF/tiles.xml</value>
                     </list>
              </property>
       </bean>


2.   Create tiles definitions



 <!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
       "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
 <tiles-definitions>

       <definition name="myTemplate" template="/views/templates/myTemplate.jsp">
              <put-attribute name="body" value="" />
              <put-attribute name="header" value="/views/templates/header.jsp" />
              <put-attribute name="footer" value="/views/templates/footer.jsp" />
       </definition>

       <definition name="index" extends="myTemplate">
              <put-attribute name="body" value="/views/templates/blank.jsp" />
       </definition>

 </tiles-definitions>


3.   Create template



 <html>
 <head>
 <title>My app</title>
 </head>
 <body>
       <div class="container">
              <tiles:insertAttribute name="header" />
              <div id="body-class" class="banner">
                     <tiles:insertAttribute name="body" />
              </div>
       </div>
       <br />
       <div>
              <div class="container" style="padding-top: 50px;">
                     <tiles:insertAttribute name="footer" />
              </div>
       </div>
 </body>

 </html>




References:

Spring Documentation


Spring MVC - Create Sample Project with Eclipse and run it on JBoss 7


Project Setup
In this tutorial I’ll use Spring Tool Suite 3.1 and JBoss 7.1 Final but they are not required. You can use IDE and application server of your choice and configure it by yourself.

Follow the steps below:
       1.   Click Alt + Shift +N or File -> New and choose Project…


       2.   Spring -> Spring Template Project -> Next


       3.   Choose Spring MVC Project , click Next and confirm Yes


       4.   Insert your project name and default java package and click Finish



Your project will be created from predefined template. It is ready to use and we’ll test it on JBoss 7.

Setup Application Server in STS
You need to download JBoss from here and unpack it on your local file system. Then follow the steps:
       1.   Go to Servers tab in STS

       2.   Right click -> New -> Server


       3.   Find JBoss 7.1 in the tree. If it is not here click Download additional     adapters and install plugin for this concrete container.  Change server host name and  server name if you want and click Next


       4.   Select your JBoss location on your hard drive and jdk’s installation. Click        Finish.



JBoss is ready to run.

Deploy Application on Server
To deploy Spring MVC app on JBoss 7 we’ll use eclipse plugins support. It will automatically build, compile project and copy war file to jboss deployments folder.
       1.   Run JBoss


       2.   Using Drag and Drop put your app on server icon.


       3.   See logs

INFO  [org.springframework.web.servlet.DispatcherServlet] (MSC service thread 1-3) FrameworkServlet 'appServlet': initialization completed in 657 ms
INFO  [org.jboss.web] (MSC service thread 1-3) JBAS018210: Registering web context: /springMVCtutorial
INFO  [org.jboss.as.server] (DeploymentScanner-threads - 1) JBAS018559: Deployed "springMVCtutorial.war"

Application is deployed

       4.   Go to web browser and go to http://localhost:8080/springMVCtutorial/

Import existing project
Generally you will be not creating new project using Eclipse / STS wizards. Probably you will import it from existing sources. To use eclipse deployment support imported maven project needs to be configured in STS. Environment has to know that this is a web module and can be deployed to server.

       1.   File -> Import…


       2.   Select Existing Maven Projects… and click Next


       3.   Set root directory and Finish



To make imported project be sure that project facets are selected like on the picture below. (project Properties -> Projects Facets)



poniedziałek, 1 kwietnia 2013

Utilizing System Properties with Jboss 6.x/7.x and Spring


JBoss gives us possibility to set our application properties / configuration as a java system property. It is very useful for configuring enterprise apps per environment. Usage of this feature is different between JBoss versions. I’ll show you how to utilize it on JBoss AS 6 and 7. Then you will see the most valuable thing, which is SystemPropertiesService from Spring Framework.

JBoss 6.x
Create xml file (deleult name is properties-service.xml ) like shown below and copy it to %JBOSS_HOME%/server/default/deploy/ folder
<server>
  <mbean code="org.jboss.varia.property.SystemPropertiesService" 
  name="jboss:type=Service,name=SystemProperties">
    <!-- 
       | Load properties from each of the given comma seperated URLs
    <attribute name="URLList">
      http://somehost/some-location.properties,
      ./conf/somelocal.properties
    </attribute>
    -->
    <attribute name="Properties">
      gkolpu.blog.example1=MY_Property1
      gkolpu.blog.example2=MY_Property2
    </attribute>
  </mbean>
</server>

JBoss 7.x
Add the code attached below to standalone.xml file, it should be added after <extensions> tag.

<system-properties>
<property name="gkolpu.blog.example1" value="MY_Property1"/>
<property name="gkolpu.blog.example2" value="MY_Property2"/>
</system-properties>
Spring Configuration
Add this code to your spring configuration. It will load your file and make your properties visible in the Spring context. There is one parameter to define, example below use SYSTEM_PROPERTIES_MODE_OVERRIDE which means that you can set all needed properties in the file embedded in your project but they will be override by the properties specified in JBoss configuration. You have 3 options here:
static int
SYSTEM_PROPERTIES_MODE_FALLBACK
          Check system properties if not resolvable in the specified properties.
static int
SYSTEM_PROPERTIES_MODE_NEVER
          Never check system properties.
static int
SYSTEM_PROPERTIES_MODE_OVERRIDE
          Check system properties first, before trying the specified properties.
<bean class="org.springframework.beans.factory.
                       config.PropertyPlaceholderConfigurer">
    <property name="systemPropertiesModeName" 
                 value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
    <property name="location" 
                 value="classpath:application.properties"/>
</bean>

poniedziałek, 25 lutego 2013

maven-hadoop-plugin

Submitting hadoop job on remote machine is not a complicated process but it takes a lot time, it could be 10 minutes or sometime 15. There are a lot steps to do to get the final result of map reduce and download it to local file system.

-compile map reduce code and build jar file
-upload jar to remote server via ftp
-connect to server via ssh client
-prepare input data in HDFS (usually only once)
-submit job using ‘hadoop jar…’ command
-copy map reduce output from HDFS to remote server local file system
-download output to local machine

Like I mentioned, doing those entire steps manually takes time. I was seeking for some ways to do it faster and easier. After short research, because I didn’t find any tool or solution, so I state the easier and the best way is to develop something myself. I was wondering how to do this. I could code some standalone application for doing this but it would not be enough comfortable again and require few steps from the user like switching from java IDE to another window and finding jar in file system. I thought maybe it will be better to write eclipse plugin. Everything would be in one place but would have some weakness also – no usage outside of eclipse.  Next thought was Maven. Integrated with … everything could be used in the console and what important plugin development for it is easy and pleasant, so I took this idea started development.

The result of my work looks really well. Now I submit my job by ‘one button click’. I have my maven execution configured in eclipse (hadoop:execute)



 My sample wordcount need to have maven-hadoop-plugin configured in its pom.xmll file

<build>
 <plugins>
  <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-hadoop-plugin</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <configuration>
    <host>ipAddress</host>
    <login>login</login>
    <password>password</password>
    <outputDir>output30</outputDir>
    <hdfsOutputDir>
      /books/users/gkolpu/output30
    </hdfsOutputDir>
    <hdfsInputDir>
      /books/input
    </hdfsInputDir>
    <className>
      org.gkolpu.hadoop.BookWordCounter
    </className>
    <jarName>WordCounter.jar</jarName>
   </configuration>
  </plugin>
 </plugins>
</build>



Running my maven hadoop:execute goal from eclipse I see all logs coloured in IDE console



When the job finished on remote server map reduce output is downloaded automatically to target directory




Output files can be easily opened in IDE editors




You can find this plugin with source code on my git-hub repository. There is one only one goal but this project is still under development and other maven goals are planned.

https://github.com/gkolpuc/maven-hadoop-plugin

niedziela, 17 lutego 2013

Dumping stack and heap

JVM by default gives us a lot executable applications/tools. Most of developers uses them intermittently or just don't. I would like to show two of them which make us able to dump stack and hep from specific running jvm. Why put them together? They both are very helpful in seeking memory leaks, deadlocks or some blocking or slower parts of our code. Stuck dump contains stack trace of every active thread in particular java process. JSTACK tool is responsible for it.

jstack -l $PROCESS_ID > "/home/gkolpu/stack-$PROCESS_ID-$TIME"

-m
     prints mixed mode (both Java and native C/C++ frames) stack trace.
-h
     prints a help message.

If found something in stack traces, we would probably know what is the sensitive part of code and which classes and objects is problem in, then we can analyse heap dump. JMAP will create it.
jmap  -J-d64 -dump:format=b,file=heap.$PROCESS_ID.hprof $PROCESS_ID

< no option > 
     When no option is used jmap prints shared object mappings. 
-heap
     Prints a heap summary. 
-histo
     Prints a histogram of the heap. 
-permstat
     Prints class loader wise statistics of permanent generation of Java heap.
-h
     Prints a help message.

Those both program can be found in java bin directory.

piątek, 4 stycznia 2013

Custom database logging with Jboss AS 6

I needed to persist some part of my application logs (deployed on jboss AS 6) into the oracle database. I started from looking into jboss-logging.xml file. There is much commented example code, using JMS, sending emails, file loggers and others, but unfortunately nothing about logging into db. Not a problem, I thought, just need to check jboss documentation. I was very surprised but on the jboss.org is nothing, no documentation, zero. For AS 5 or 7 - no problem, documentation is available but not AS 6 – unbelievable. Next I searched in the internet. Found not much, only e few posts on forums, few articles of log4j logging but with no Jboss AS 6 syntax which is different than previous versions and typical log4j configuration xml file. So, I wanted to share my result, It was not so easy than I excepted, so it could be usefull. First you need to add log4j-appender declaration
   <log4j-appender name="DEV_JDBC" 
  class="org.apache.log4j.jdbc.JDBCAppender">
      <error-manager>
         <only-once/>
      </error-manager>
      <level name="INFO"/>
      <properties>
         <property name="driver">
  oracle.jdbc.driver.OracleDriver
   </property>
         <property name="URL">
  jdbc:oracle:thin:@mydbhost.gkolpu.com:1521:dev
   </property>
         <property name="user">DEV</property>
         <property name="password">DEV</property>
         <property name="sql">
  insert into logging_table(id, tmstmp,message) 
  values(sequence.nextval,now(),'%m')
   </property>
         <property name="bufferSize">100</property>
      </properties>

   </log4j-appender>
And use it in logger
<logger category="org.gkolpu.logging.db" use-parent-handlers="false">
       <level name="INFO"/>
       <handlers>
         <handler-ref name="DEV_JDBC"/>
       </handlers>
     </logger>
Actually my goal was, calling stored procedure to save the log in db, by I couldn’t use JDBCAppender for that, it was not working. To fix this I coded my own appender, actually only extended JDBCAppender. Original code used Statement class to execute insert operation. This is java code (jar with it complied must be putted to jboss libs).
package org.gkolpu;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;

import org.apache.log4j.jdbc.JDBCAppender;

public class CallableJDBCAppender extends JDBCAppender {
 @Override
 protected void execute(String sql) throws SQLException {

  Connection con = null;
  CallableStatement call = null;

  try {
   con = getConnection();
   call = con.prepareCall(sql);
   call.execute();
  } finally {
   if (call != null) {
    call.close();
   }
   closeConnection(con);
  }
 }
}
And a few changes into the appender declaration.
<log4j-appender name="DEV_JDBC" 
 class="org.gkolpu.CallableJDBCAppender">
      <error-manager>
         <only-once/>
      </error-manager>
      <level name="INFO"/>
      <properties>
         <property name="driver">
  oracle.jdbc.driver.OracleDriver
   </property>
         <property name="URL">
  jdbc:oracle:thin:@mydbhost.gkolpu.com:1521:dev
   </property>
         <property name="user">DEV</property>
         <property name="password">DEV</property>
         <property name="sql">
  {call MY_PACKAGE.log_message('%m')}
   </property>
         <property name="bufferSize">100</property>
      </properties>

   </log4j-appender>
;