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

poniedziałek, 15 kwietnia 2013

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>

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>
;