[jboss-dev-forums] [JBoss ESB Development] New message: "Re: Smooks problem"

Gilles Dupont Tagne Tagne do-not-reply at jboss.com
Sat Jan 16 13:44:21 EST 2010


JBoss development,

A new message was posted in the thread "Smooks problem":

http://community.jboss.org/message/520447#520447

Author  : Gilles Dupont Tagne Tagne
Profile : http://community.jboss.org/people/tagnegilles

Message:
--------------------------------------------------------------
Thanks Maurice for your help. I fund how to solve the problem. I wrote a ESB customer action GillesSmooksAction.java
 
package de.gilles.projects.splitteresb;
 
import java.io.ByteArrayInputStream;
 
import javax.naming.InitialContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.PersistenceUnit;
import javax.xml.transform.stream.StreamSource;
 
import org.jboss.soa.esb.actions.AbstractActionLifecycle;
import org.jboss.soa.esb.actions.ActionProcessingException;
import org.jboss.soa.esb.helpers.ConfigTree;
import org.jboss.soa.esb.message.Message;
import org.milyn.Smooks;
import org.milyn.container.ExecutionContext;
import org.milyn.event.report.HtmlReportGenerator;
import org.milyn.persistence.util.PersistenceUtil;
import org.milyn.scribe.adapter.jpa.EntityManagerRegister;
 
public class GillesSmooksAction extends AbstractActionLifecycle {
     
     protected ConfigTree _configTree;
     
     public GillesSmooksAction(ConfigTree configTree){
          _configTree = configTree;
     }
     
     public Message process(Message message) throws ActionProcessingException {
          
          Smooks smooks = null;
          EntityManagerFactory emf = null;
          EntityManager em = null;
          
          try {
               String smooksConfigFile = _configTree.getAttribute("smooksConfig");
               if(smooksConfigFile == null){
                    throw new Exception("The Attribute with the name: 'smooksConfig' doesn't exist." );
               }
               
//               emf = Persistence.createEntityManagerFactory("persondb");
//               em = emf.createEntityManager();
               InitialContext jndiContext = new InitialContext();
               emf = (EntityManagerFactory) jndiContext.lookup("java:/personDbEntityManagerFactory");
               em = emf.createEntityManager();
               smooks = new Smooks(smooksConfigFile);
               ExecutionContext executionContext = smooks.createExecutionContext();
               executionContext.setEventListener(new HtmlReportGenerator("/Users/gilles/Desktop/report/reportjpa.html"));
               PersistenceUtil.setDAORegister(executionContext, new EntityManagerRegister(em));
               EntityTransaction tx = em.getTransaction();
               tx.begin();
               smooks.filterSource(executionContext, new StreamSource(new ByteArrayInputStream(((String)message.getBody().get()).getBytes())));
               tx.commit();
               
          } catch (Exception e) {
               
               e.printStackTrace();
               throw new ActionProcessingException(e);               
          } 
          finally{
               
               try {
                    if(em != null) em.close();
               } catch (Exception e2) {}
               
               try {
                    if(emf != null) emf.close();
               } catch (Exception e2) {}
               
               if(smooks != null) smooks.close();
          }
          return message;
     }
     
   public void exceptionHandler(Message message, Throwable exception) {
        logHeader();
        System.out.println("!ERROR!");
        System.out.println(exception.getMessage());
        System.out.println("For Message: ");
        System.out.println(message.getBody().get());
        logFooter();
   }
     
 
   // This makes it easier to read on the console
   private void logHeader() {
        System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
   }
   private void logFooter() {
        System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n");
   }
   
}
 

 
Then i changed the jboss-esb.xml, persistence.xml and the dbSmooksConf.xml
 
jboss-esb.xml
<?xml version="1.0"?>
<jbossesb parameterReloadSecs="5"
 xmlns="http://anonsvn.labs.jboss.com/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.2.0.xsd"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://anonsvn.labs.jboss.com/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.2.0.xsd http://anonsvn.labs.jboss.com/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.2.0.xsd">
 <providers>
  <jms-provider connection-factory="ConnectionFactory" name="JbossMessaging">
   <jms-bus busid="splitterChannel">
    <jms-message-filter dest-name="/queue/splitterQueueAware" dest-type="QUEUE"/>
   </jms-bus>
  </jms-provider>
  <schedule-provider name="CronSchedulerProvider">
   <cron-schedule cronExpression="0/3 * * * * ?" scheduleid="myCronSchedule"/>
  </schedule-provider>
  <fs-provider name="FileSystemProvider">
   <fs-bus busid="esbFileSystem">
    <fs-message-filter directory="/Users/gilles/Desktop/ESB/Input"
     error-delete="false"
     error-directory="/Users/gilles/Desktop/ESB/Error"
     error-suffix=".error" input-suffix=".csv" post-delete="true"
     post-directory="/Users/gilles/Desktop/ESB/Post" post-suffix=".post" work-suffix=".work"/>
   </fs-bus>
  </fs-provider>
 </providers>
 <services>
  <service category="splitterTest" description="Splitt the .csv file"
   invmScope="GLOBAL" name="fileSplitter">
   <listeners>
    <fs-listener busidref="esbFileSystem" is-gateway="true"
     name="FileListener" scheduleidref="myCronSchedule">
     <property name="composer-class" value="org.jboss.soa.esb.smooks.splitting.FileStreamSplitter"/>
     <property name="splitterConfig" value="/mySmooksConf.xml"/>
    </fs-listener>
   </listeners>
   <actions mep="OneWay">
    <action class="org.jboss.soa.esb.actions.SystemPrintln" name="Print">
     <property name="message" value="[Splitter] Message Split complete"/>
    </action>
   </actions>
  </service>
  <service category="splitterTest"
   description="Route the message to a jms queue" invmScope="GLOBAL" name="routerSplitter">
   <actions mep="OneWay">
    <action class="org.jboss.soa.esb.actions.Notifier" name="NotifyAction1">
     <property name="destinations">
      <NotificationList type="ok">
       <target class="NotifyQueues">
        <queue jndiName="queue/splitterQueue"/>
       </target>
      </NotificationList>
     </property>
     <property name="okMethod" value="notifyOK"/>
    </action>

   </actions>
  </service>
  <service category="splitterTest"
   description="Route the age element of the message to a jms queue"
   invmScope="GLOBAL" name="routerAgeSplitter">
   <actions mep="OneWay">
    <action class="org.jboss.soa.esb.actions.Notifier" name="NotifyAction2">
     <property name="destinations">
      <NotificationList type="ok">
       <target class="NotifyQueues">
        <queue jndiName="queue/splitterAgeESBQueue"/>
       </target>
      </NotificationList>
     </property>
     <property name="okMethod" value="notifyOK"/>
    </action>

   </actions>
  </service>
  <service category="splitterTest" description="Make routing on Age"
   invmScope="GLOBAL" name="recipientListAge">
   <actions mep="OneWay">
    <action class="org.jboss.soa.esb.actions.StaticRouter" name="staticRoutingAge">
     <property name="destinations">
      <route-to destination-name="ToRouterAgeSplitter"
       service-category="splitterTest" service-name="routerAgeSplitter"/>
      <route-to destination-name="ToDbPersistAge"
       service-category="splitterTest" service-name="dbPersist"/>
     </property>
    </action>
   </actions>
  </service>
  <service category="splitterTest"
   description="Persist data in the database" invmScope="GLOBAL" name="dbPersist">

   <actions mep="OneWay">
    <action class="org.jboss.soa.esb.actions.SystemPrintln" name="PrintMessage1">
     <property name="message" value="Message"/>
    </action>
    <action class="org.jboss.soa.esb.smooks.SmooksAction" name="smoksActionXML">
     <property name="smooksConfig" value="/xmlSmooksConf.xml"/>
    </action>
    <action class="org.jboss.soa.esb.actions.SystemPrintln" name="PrintMessage2">
     <property name="message" value="Message"/>
    </action>

     <!-- GillesSmooksAction -->
    <action class="de.gilles.projects.splitteresb.GillesSmooksAction" name="dbPersist">
     <property name="smooksConfig" value="/dbSmooksConf.xml"/>
    </action>


   </actions>
  </service>
 </services>
</jbossesb>


 
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd ">
     <persistence-unit name="persondb" transaction-type="RESOURCE_LOCAL">
          <non-jta-data-source>java:/SplitterSqlDB</non-jta-data-source>
          <class>de.gilles.projects.splitteresb.PersonAge</class>
          <properties>
               <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
               <property name="hibernate.hbm2ddl.auto" value="create-drop" />     
               <property name="jboss.entity.manager.factory.jndi.name" value="java:/personDbEntityManagerFactory"/>     
          </properties>
     </persistence-unit>
</persistence>


 
dbSmooksConf.xml
<?xml version="1.0" encoding="UTF-8"?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
     xmlns:jb="http://www.milyn.org/xsd/smooks/javabean-1.2.xsd" 
     xmlns:dao="http://www.milyn.org/xsd/smooks/persistence-1.2.xsd">
     
    <params>        
        <param name="stream.filter.type">SAX</param>
    </params>

     <jb:bean beanId="personage" class="de.gilles.projects.splitteresb.PersonAge" createOnElement="person">
          <jb:value property="id" data="person/id" decoder="Integer"/>
          <jb:value property="age" data="person/age" decoder="Integer"/>
     </jb:bean>              

     <dao:inserter beanId="personage" insertOnElement="person"/>  
     
</smooks-resource-list>

 
It works

--------------------------------------------------------------

To reply to this message visit the message page: http://community.jboss.org/message/520447#520447




More information about the jboss-dev-forums mailing list