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)



sobota, 13 kwietnia 2013

Project Coin - Java 7 features


Version 7 of Java implements few very useful features known as a part of a project coin. I agree, we need them and but changes to the language are pretty cosmetic. Closures is the thing which all Java developers around the world expected, we have to be patient, It will come up in version 8. But this is another story.
In this post I want to show you what the direct language changes in Java 7 are.

1.   STRINGS IN SWITCH



  (number) {
              case "One":
                     logger.info("Your number is 1");
                     break;
              case "Two":
                     logger.info("Your number is 2");
                     break;
              case "Three":
                     logger.info("Your number is 3");
                     break;
              case "Four":
                     logger.info("Your number is 4");
                     break;
              default:
                     logger.info("Error: wrong number");
                     break;
          }


2.   BINARY LITERALS



  int x = Integer.parseInt("1100110", 2);

  int x1 = 0b1100110;


3.   UNDERSCORES IN NUMBERS



  long longNumber = 2_147_483_648L;
  int binaryNumber = 0b0001_1100__0011_0111__0010_1011__1010_0011;


4.   EXCEPTION HANDLING



              try {
                     ...
              } catch (FileNotFoundException | ParseException
                           | ConfigurationException e) {
                     System.err.println("Config file '" + fileName
                                  + "' is missing or malformed");
              }


5.   DIAMOND SYNTAX
Don't need to evaluate generic types like in Java 6 



  //JAVA 6
  HashMap<StringStringhm1 = new HashMap<StringString>();




  //JAVA 7
  HashMap<String, String> hm2 = new HashMap<>();


6.   TRY WITH RESOURCES



  try (FileInputStream fstream = new FileInputStream(fileName);
       DataInputStream in = new DataInputStream(fstream);
       BufferedReader br = new BufferedReader(new InputStreamReader(in));) {

                     String strLine;
                     while ((strLine = br.readLine()) != null) {
                           lines.add(strLine);
                     }

              } catch (FileNotFoundException e) {
                     logger.error("FileNotFoundException", e);
              } catch (IOException e) {
                     logger.error("IOException", e);
              }


7.   GENERIC VARARGS WARN
This is small change but also important. Compiler warn us on the compile time that we are creating the array generic type which cannot be done type-safely.




  public static <T> Collection<T> doSomething(T... entries) {
              ...
       }




Now we can use new java annotation to let compiler not warn us any more in this place : @SafeVarargs


References:

The Well-Grounded Java Developer: Vital techniques of Java 7 and polyglot programming