Matteo Redaelli

Computer Science OpenSource Linux Africa Volontariato Ambiente Energia solare Mezzi Pubblici Piste ciclabili

Connecting to a database with a webService (cxf) deployed (jbi) in Servicemix 30 May 2009

Filed under: Me — Matteo @ 11:53
Tags: , , , ,

See the newer post at www.redaelli.org

Introduction

Apache ServiceMix at the moment doesn’t have a JBI component for connecting to a database (servicemiix-jdbc is in the roadmap): in the meanwhile you have to use servicemix-cxf or servicemix-bean.

This example (cxf-wsdl-first-jdbc.zip, cxf-wsdl-first.pdf)  is built on top of the example cxf-wsdl-first and can be deployed in apache servicemix 4.0 as JBI sa package.

Installing smx4

Download from “http://servicemix.apache.org/SMX4/download.html

Unzip it

cd /usr/local

tar xvfz apache-servicemix.tar.gz

Starting smx4

cd /usr/local/apache-servicemix-4.0.0/

./bin/servicemix

Creating WS

cd /usr/local/apache-servicemix-4.0.0/examples

cp -a cxf-wsdl-first cxf-wsdl-first-jdbc

Edit file “wsdl-first-cxfse-su/pom.xml”

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>2.5.6</version>
</dependency>

Edit file “wsdl-first-cxfse-su/src/main/resources/xbean.xml”

<bean id="moodleDB"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/moodle" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<cxfse:endpoint>
<cxfse:pojo>
<bean class="org.apache.servicemix.samples.wsdl_first.PersonImpl" >
<property name="dataSource" ref="moodleDB"/>
</bean>
</cxfse:pojo>
</cxfse:endpoint>

Edit file “wsdl-first-cxfse-su/src/main/java/org/apache/servicemix/samples/wsdl_first/PersonImpl.java”

import java.util.*;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
..
private DataSource dataSource;
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) {
 this.dataSource = dataSource;
 this.jdbcTemplate = new JdbcTemplate(dataSource);
}
public void getPerson(Holder<String> personId, Holder<String> ssn, Holder<String> name)
throws UnknownPersonFault
{
org.apache.servicemix.samples.wsdl_first.types.UnknownPersonFault fault =
  new org.apache.servicemix.samples.wsdl_first.types.UnknownPersonFault();
fault.setPersonId(personId.value);
if (personId.value == null || personId.value.length() == 0) {
   throw new UnknownPersonFault(null, fault);
}
List result = this.jdbcTemplate.queryForList("select description, lastname from mdl_user where username = ?",
   new Object[]{personId.value});
if(result.size() != 1)
 throw new UnknownPersonFault(null, fault);
 Map record=(Map)result.get(0);
 name.value = record.get("lastname").toString();
 ssn.value = record.get("description").toString();
}

Deplying the WS

mvn clean instal

cp wsdl-first-cxf-sa/target/wsdl-first-cxf-sa-4.0.0.zip /usr/local/apache-servicemix-4.0.0/deploy/

Testing WS

Open with your browser the file “file:///usr/local/apache-servicemix-4.0.0/examples/cxf-wsdl-first-jdbc/client.html”

 

2 Responses to “Connecting to a database with a webService (cxf) deployed (jbi) in Servicemix”

  1. Safi Says:

    Hi! i tried to uss this example the compilation is failed; i got this error when i type mvn clean instal :

    [INFO] Scanning for projects…
    [WARNING]
    [WARNING] Some problems were encountered while building the effective model for org.apache.servicemix.examples.cxf-wsdl-first:wsdl-first-cxfse-su:jbi-service-unit:4.2.0-fuse-02-00
    [WARNING] ‘repositories.repository.layout’ for java.net uses the unsupported value ‘legacy’, artifact resolution might fail. @ org.apache.servicemix.features:features:4.2.0-fuse-02-00, /Users/abdelsalam/.m2/repository/org/apache/servicemix/features/features/4.2.0-fuse-02-00/features-4.2.0-fuse-02-00.pom, line 471, column 19
    [WARNING]
    [WARNING] Some problems were encountered while building the effective model for org.apache.servicemix.examples.cxf-wsdl-first:wsdl-first-cxfbc-su:jbi-service-unit:4.2.0-fuse-02-00
    [WARNING] ‘repositories.repository.layout’ for java.net uses the unsupported value ‘legacy’, artifact resolution might fail. @ org.apache.servicemix.features:features:4.2.0-fuse-02-00, /Users/abdelsalam/.m2/repository/org/apache/servicemix/features/features/4.2.0-fuse-02-00/features-4.2.0-fuse-02-00.pom, line 471, column 19
    [WARNING]
    [WARNING] Some problems were encountered while building the effective model for org.apache.servicemix.examples.cxf-wsdl-first:wsdl-first-cxf-sa:jbi-service-assembly:4.2.0-fuse-02-00
    [WARNING] ‘repositories.repository.layout’ for java.net uses the unsupported value ‘legacy’, artifact resolution might fail. @ org.apache.servicemix.features:features:4.2.0-fuse-02-00, /Users/abdelsalam/.m2/repository/org/apache/servicemix/features/features/4.2.0-fuse-02-00/features-4.2.0-fuse-02-00.pom, line 471, column 19
    [WARNING]
    [WARNING] Some problems were encountered while building the effective model for org.apache.servicemix.examples.cxf-wsdl-first:wsdl-first-client:jar:4.2.0-fuse-02-00
    [WARNING] ‘build.plugins.plugin.version’ for org.codehaus.mojo:exec-maven-plugin is missing. @ line 51, column 21
    [WARNING] ‘repositories.repository.layout’ for java.net uses the unsupported value ‘legacy’, artifact resolution might fail. @ org.apache.servicemix.features:features:4.2.0-fuse-02-00, /Users/abdelsalam/.m2/repository/org/apache/servicemix/features/features/4.2.0-fuse-02-00/features-4.2.0-fuse-02-00.pom, line 471, column 19
    [WARNING]
    [WARNING] Some problems were encountered while building the effective model for org.apache.servicemix.examples:cxf-wsdl-first:pom:4.2.0-fuse-02-00
    [WARNING] ‘build.pluginManagement.plugins.plugin.(groupId:artifactId)’ must be unique but found duplicate declaration of plugin org.apache.maven.plugins:maven-surefire-plugin @ org.apache.servicemix.features:features:4.2.0-fuse-02-00, /Users/abdelsalam/.m2/repository/org/apache/servicemix/features/features/4.2.0-fuse-02-00/features-4.2.0-fuse-02-00.pom, line 923, column 23
    [WARNING] ‘repositories.repository.layout’ for java.net uses the unsupported value ‘legacy’, artifact resolution might fail. @ org.apache.servicemix.features:features:4.2.0-fuse-02-00, /Users/abdelsalam/.m2/repository/org/apache/servicemix/features/features/4.2.0-fuse-02-00/features-4.2.0-fuse-02-00.pom, line 471, column 19
    [WARNING]
    [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
    [WARNING]
    [WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
    [WARNING]
    [INFO] ————————————————————————
    [INFO] Reactor Build Order:
    [INFO]
    [INFO] ServiceMix :: Samples :: CXF WSDL first
    [INFO] ServiceMix :: Samples :: WSDL first :: CXF SE
    [INFO] ServiceMix :: Samples :: WSDL first :: CXF BC
    [INFO] ServiceMix :: Samples :: WSDL first :: SA
    [INFO] ServiceMix :: Samples :: WSDL first :: Client
    [INFO]
    [INFO] ————————————————————————
    [INFO] Building ServiceMix :: Samples :: CXF WSDL first 4.2.0-fuse-02-00
    [INFO] ————————————————————————
    [INFO] ————————————————————————
    [INFO] Reactor Summary:
    [INFO]
    [INFO] ServiceMix :: Samples :: CXF WSDL first ……….. FAILURE [0.151s]
    [INFO] ServiceMix :: Samples :: WSDL first :: CXF SE ….. SKIPPED
    [INFO] ServiceMix :: Samples :: WSDL first :: CXF BC ….. SKIPPED
    [INFO] ServiceMix :: Samples :: WSDL first :: SA ……… SKIPPED
    [INFO] ServiceMix :: Samples :: WSDL first :: Client ….. SKIPPED
    [INFO] ————————————————————————
    [INFO] BUILD FAILURE
    [INFO] ————————————————————————
    [INFO] Total time: 5.674s
    [INFO] Finished at: Wed Jun 01 11:43:46 CET 2011
    [INFO] Final Memory: 5M/81M
    [INFO] ————————————————————————
    [ERROR] Unknown lifecycle phase “instal”. You must specify a valid lifecycle phase or a goal in the format : or :[:]:. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-site, site, post-site, site-deploy, pre-clean, clean, post-clean. -> [Help 1]
    [ERROR]
    [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR]
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/LifecyclePhaseNotFoundException

    plz help

  2. M. Says:

    Maybe would you try “mvn clean install”.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s