Mule and web services

26
MULE AND WEB MULE AND WEB SERVICES SERVICES By - Prabhat gangwar

Transcript of Mule and web services

Page 1: Mule and web services

MULE AND WEB MULE AND WEB SERVICESSERVICES

By - Prabhat gangwar

Page 2: Mule and web services

Bookstore

Publisher

Our scenarioOur scenario

Bookstore Service

+ addBooks

+ getBooks

Bookstore ClientBook list

(CSV)

Buyer

Bookstore Client

Email order

confirmation

Page 3: Mule and web services

How?How?Mule provides

◦File connector◦Email connector◦CXF connector

What’s CXF?◦Successor to XFire ◦Apache web services framework◦JAX-WS compliant◦SOAP, WSDL, WS-I Basic Profile◦WS-Addressing, WS-Security, WS-Policy,

WS-ReliableMessaging

Page 4: Mule and web services

How?How?1. Build web service2. Configure Mule server3. Generate web service client4. Build CSV->Book converter5. Configure Mule with File inbound

router and CXF outbound router6. Configure Mule with Email endpoint

to confirm orders7. Send messages to email service

from bookstore

Page 5: Mule and web services

Quick Intro to JAX-WSQuick Intro to JAX-WSThe standard way to build SOAP

web servicesSupports code first web service

building through annotationsSupports WSDL first through

ant/maven/command line tools

Page 6: Mule and web services

Building the Web ServiceBuilding the Web Service public interface Bookstore { long addBook(Book book); Collection<Long> addBooks(Collection<Book>

books); Collection<Book> getBooks(); }

Page 7: Mule and web services

Annotating the serviceAnnotating the service@WebServicepublic interface Bookstore { long addBook(@WebParam(name="book") Book book); @WebResult(name="bookIds") Collection<Long> addBooks( @WebParam(name="books") Collection<Book> books); @WebResult(name="books") Collection<Book> getBooks();

void placeOrder(@WebParam(name=“bookId”) long bookId, @WebParam(name=“address”)String address, @WebParam(name=“email”)String email)}

Page 8: Mule and web services

Data Transfer ObjectsData Transfer Objectspublic class Book { get/setId get/setTitle get/setAuthor}

Page 9: Mule and web services

ImplementationImplementation@WebService(serviceName="BookstoreService", portName="BookstorePort“, endpointInterface="org.mule.example.bookstore.Bookstore")public class BookstoreImpl implements Bookstore {…}

Page 10: Mule and web services

Configuring MuleConfiguring Mule <mule-descriptor name="echoService"

implementation="org.mule.example.bookstore.BookstoreImpl">

<inbound-router> <endpoint address="cxf:http://localhost:8080/services/bookstore"/> </inbound-router>

</mule-descriptor>

Page 11: Mule and web services

Starting MuleStarting MuleWeb applicationsEmbeddedSpringAnywhere!

Page 12: Mule and web services

Main.main() – simplicityMain.main() – simplicityMuleXmlConfigurationBuilder builder

= new MuleXmlConfigurationBuilder();UMOManager manager =

builder.configure("bookstore.xml");

Page 13: Mule and web services

What happened?What happened?

Page 14: Mule and web services

Some notes about web Some notes about web servicesservices

Be careful about your contracts!!!Don’t couple your web service too

tightly (like this example), allow a degree of separation

This allows◦Maitainability◦Evolvability◦Versioning

WSDL first helps this

Page 15: Mule and web services

The PublisherThe PublisherWants to read in CSV and publish to

web serviceUse a CXF generated client as the

“outbound router”CsvBookPublisher converts File to

List<Book> Books then get sent to outbound

router

Page 16: Mule and web services

Our implementationOur implementation public class CsvBookPublisher { public Collection<Book> publish(File file) throws IOException { … } }

Page 17: Mule and web services

Domain objects are the Domain objects are the MessagesMessagesFileBook

◦Title◦Author

Access can be gained to raw UMOMessage as well

Page 18: Mule and web services

Generating a clientGenerating a client<plugin> <groupId>org.apache.cxf</groupId> <artifactId>cxf-codegen-plugin</artifactId> <version>2.0.2-incubator</version> <executions> <execution> <id>generate-sources</id> <phase>generate-sources</phase> <configuration> <sourceRoot> ${basedir}/target/generated/src/main/java </sourceRoot> <wsdlOptions> <wsdlOption> <wsdl> http://localhost:8080/services/bookstore?wsdl </wsdl> </wsdlOption> </wsdlOptions> </configuration> <goals> <goal>wsdl2java</goal> </goals> </execution> </executions></plugin>

Page 19: Mule and web services

Our configurationOur configuration <mule-descriptor name="csvPublisher" implementation="org.mule.example.bookstore.publisher.CsvBookPublisher" singleton="true">

<inbound-router> <endpoint address="file://./books?

pollingFrequency=100000&amp;autoDelete=false"/> </inbound-router> <outbound-router> <router

className="org.mule.routing.outbound.OutboundPassThroughRouter"> … </router> </outbound-router>

</mule-descriptor>

Page 20: Mule and web services

Outbound router Outbound router configurationconfiguration <router

className="org.mule.routing.outbound.OutboundPassThroughRouter">

<endpoint address="cxf:http://localhost:8080/services/bookstore"> <properties> <property name="clientClass" value="org.mule.example.bookstore.BookstoreService"/> <property name="wsdlLocation" value="http://localhost:8080/services/bookstore?wsdl"/> <property name="port" value="BookstorePort"/> <property name="operation" value="addBooks"/> </properties> </endpoint>

</router>

Page 21: Mule and web services

Start it up!Start it up!

Page 22: Mule and web services

Email IntegrationEmail Integration

Bookstore

Place Order

Order (Book, Email,

Address)

Order to email transformer

Object to Message

transformer

SMTP endpoint

Page 23: Mule and web services

Endpoint ConfigurationEndpoint Configuration <global-endpoints> <endpoint name="orderEmailService" address="smtps://username:password@hostname" transformers="OrderToEmail ObjectToMimeMessage"> <properties> <property name="fromAddress" value="[email protected]" /> <property name="subject“ value="Your order has been placed!" /> </properties> </endpoint> </global-endpoints>

Page 24: Mule and web services

TransformersTransformers <transformers> <transformer name="OrderToEmail" className="org.mule.example.bookstore.OrderToEmailTransformer"/> <transformer name="ObjectToMimeMessage"

className="org.mule.providers.email.transformers.ObjectToMimeMessage"/>

</transformers>

Page 25: Mule and web services

Sending the MessageSending the Message public void orderBook(long bookId, String address, String email) { // In the real world we'd want this hidden behind // an OrderService interface try { Book book = books.get(bookId); MuleMessage msg = new MuleMessage( new Object[] { book, address, email} );

RequestContext.getEventContext().dispatchEvent( msg, "orderEmailService"); System.out.println("Dispatched message to orderService."); } catch (UMOException e) { // If this was real, we'd want better error handling throw new RuntimeException(e); } }

Page 26: Mule and web services

Questions?Questions?My Blog: http://netzooid.comProject Site:

http://mule.mulesource.orgMuleForge: http://muleforge.orgMuleSource: http://mulesource.com