JAX-WS Web Service with JBoss Drools and Apache jUDDI
1. Building a JAX-WS Web Service with JBoss Drools and Apache jUDDI Integration JavaOne 2010 Session: S313909 Monday, September 20, 2010
2. About Your Speakers Dave Read CTO CISSP, RHCE, SCJP David.Read@blueslate.net Paul Evans Chief Architect SCJD, SCWCD Paul.Evans@blueslate.net 1 Smart Approach. Extraordinary Results. www.blueslate.net
3. Why Are We Here? To walk through the architecture and design of a Java-based web service service based on a set requirements JAX-WS, JAXB, JAXR, JSR94,… To demonstrate how the architecture of a complex web service flows from object-oriented principals 2
4. Agenda The Requirements Architecture of Proposed Solution Major Components Demo Summary Q/A 3
5. Agenda The Requirements Architecture of Proposed Solution Major Components Demo Summary Q/A 4
6. Functional Requirements FR.1: Require the ability to calculate the sub-total price of a book order (in support of the to-be developed book order web portal) FR.2: Require the order sub-total to be broken-down by line item - the order sub-total is simply the summation of the line item sub-totals FR.3: The sub-total of each line item is calculated by multiplying the price of the book with the quantity, then factoring-in sales tax and factoring-in shipping costs FR.4: Discounts may be applied to the order sub-total based on the customer's loyalty level (other rules may be crafted in the future that will impact the sub-total calculation) 5
7. Non-functional Requirements NFR.1: The rules governing the calculation of the sub-total should be maintainable by business analysts New rules can be added, existing rules to be modifiable without involving the application development team NFR.2: The order total processing system should be componentized and not tied to a web service context NFR.4: The sub-total processing system should be easy and convenient for consumers to utilize NFR.7: The rule engine will be limited to the execution of business rules - it should not be used for “mechanical” type purposes 6
8. Agenda The Requirements Architecture of Proposed Solution Major Components Demo Summary Q/A 7
9. Architecture of Proposed Solution (logical) 8 Service Registry Consumer Order Total Service Order Total Service Client BRE Connector Service Registry Connector EP Bind EP Bind EP Bind Business Rule Engine (BRE) Rule Definitions
10. Notables Missing from Logical Architecture Enterprise Service Bus Complex piece of system software Omitted for brevity Logging service Logging service to be used enterprise-wide A logging service would operate asynchronously for snappy performance Regardless of the number of tiers or clustering, log messages would be recorded by the logging service to a single system for ease of auditing and monitoring Security service Handles authentication and authorization requests Queried to ascertain encryption and integrity requirements of a service 9
11. Reconcile Requirements with Logical Architecture (1/4) FR.1, FR.4: The service implementation will handle the mechanics of totaling the individual line items and other costs to produce the order total. This includes adding shipping cost, adding total tax and applying discounts produced by the BRE. FR.1, FR.2,FR.4: The rule engine houses and executes the core business rules in support of calculating the order total. The BRE houses rules associated with the calculation of sales tax, discounts, shipping cost, etc. (it is the service implementation that is responsible for performing the raw mathematics to calculate the total) Order Total Service Business Rule Engine (BRE) Rule Definitions 10
12. Quick Note on Rules and Rule Engines (1/2) 11 What? Execution environment for business rules Storage / housing of business rules Environment for authoring and maintaining business rules Usually leverages Rete algorithm for rule execution Think of as just another component within the architecture – RDBMS component = data persistence LDAP component = identity management (authentication / authorization) Mainframe component = legacy transaction processing BRE component = execution of business rules Workflow engine component = codification of workflow … = …
13. Quick Note on Rules and Rule Engines (2/2) 12 Why? Interprets business data data sitting in a relational database in and of itself is meaningless – SOMETHING needs to give the data MEANING business rules provide meaning to data Codification of business rules in a format that is closer to the business domain Rules codified in low-level programming languages (e.g. 3GLs such as Java, C++, etc) are expensive to maintain and difficult/impossible for business users to validate BREs typically provide an environment to codify rules using a grammar that is more natural to technical-savvy business users Other reasons Easier to audit Expose as a service to enable other systems the ability to leverage common rule base …
14. Reconcile Requirements with Logical Architecture (2/4) 13 NFR.2: Providing a connector to the BRE simplifies integration – the connector increases the accessibility of the BRE to other consumers. Multiple connectors can be created to support a diverse ecosystem of heterogeneous potential consumers. BRE Connector NFR.1: BREs allow rules to be codified using a language close to the business domain NFR.1: BREs typically provide a GUI to allow non-programmers the ability to manage rules Business Rule Engine (BRE) Rule Definitions
15. Reconcile Requirements with Logical Architecture (3/4) 14 Using a service registry provides a level of indirection that enables services to be loosely coupled from consumers from the dimension of location. Service Registry NFR.4: The service registry connector is used to connect to a service registry and discover the physical endpoint of some target service. Order Total Service Client Service Registry Connector
16. Quick Note on Registries 15 Publication of web services Discovery and lookup of data formats, messages, business processes, services ebXML Started by OASIS and United Nations (UN/CEFACT) in 1999 Repository of data formats, process definitions, messages across industries Enable global world to participate in electronic commerce (including developing nations) UDDI Sponsored by OASIS – 2000/2001 Enables business to publish and lookup service listings Peculiar to web services
17. Reconcile Requirements with Logical Architecture (4/4) 16 NFR.4: The service client shields the consumer from the details and mechanics of invoking the web service. Consumer Order Total Service Client Service Registry Connector Bind Bind Bind
21. Usage of service registry insulates consumers from service endpoint locations
22.
23. Java domain model created by hand (is not auto-generated by JAXB compiler from canonical XSDs)
24.
25. Physical Architecture of ProcessSubTotal Web Service 20 ProcessTotal Web Service JAX-WS Runtime (+) Dispatcher Servlet Book Order / ProcessTotal SEI Implementation @WebServiceProvider annotation Book Order / ProcessTotal WSDL Book Order Marshaler / Un-marshalerComponent (JAXB) Book Order Commands canonical (XSD) Context Neutral Order Rule Access Object (RAO)Component Book Order Entities canonical (XSD) Rule Engine ConnectorComponent (JSR-94) Book Order CommandsComponent Context Neutral Context Neutral Context Neutral Context Neutral Book Order Domain ModelComponent (POJOs) Business Rule Engine JBoss Rules (Drools) Context Neutral
26. Physical Architecture of Client Application JAX-WS Runtime (+) Dispatcher Servlet ProcessTotalWeb Service ProcessTotal SEI Implementation @WebServiceProvider annotation Book Order - ProcessTotal WSDL ProcessTotalService Component (Java SE) Book Order Marshaler / Un-marshalerComponent (JAXB) Book Order Commands canonical (XSD) SOAP/HTTP CONTEXT NEUTRAL CONTEXT NEUTRAL Rule Engine ConnectorComponent (JSR-94) Book Order Entities canonical (XSD) Book Order Domain ModelComponent (POJOs) CONTEXT NEUTRAL CONTEXT NEUTRAL CONTEXT NEUTRAL Business Rule Engine JBoss Rules (Drools) Client Application Service Registry Connector (JAXR) ProcessTotal Service Client Component Context Neutral JAX-WS Dispatch mechanism UDDI Registry Apache jUDDI Book Order Marshaler / Un-marshalerComponent (JAXB) Book Order CommandsComponent Context Neutral Context Neutral Service Registry (returns endpoint URL of ProcessTotal service) Book Order Commands canonical (XSD) Book Order Domain ModelComponent (POJOs) Book Order Entities canonical (XSD) Book Order Domain ModelComponent (POJOs) Context Neutral Context Neutral 21 Context Neutral 21
46. JAX-WS (Java API for XML Web Services) Evolution from JAX-RPC Supports the development of both bottom-up and top-down (contract first) services Data binding is delegated to JAXB Provides WSDL / Java mapping Makes extensive use of annotations Provides service-side and client-side APIs Handler framework 27
47. JAX-WS Service Endpoint Implementation (SEI) Java class to process requests to web service endpoint Must carry javax.jws.WebService annotation Maps to a wsdl:portType container element Methods of this class may carry javax.jws.WebMethod annotation JAX-WS runtime will leverage JAXB to handle un-marshalling and marshalling of inbound request and response Very simple! 28
48. However…we need more control We have requirements that stipulate the components we build should be reusable; and be reusable outside the context of web service 29 Context usage scenarios Web Services Messaging EJB EDI … XML message Canonical XSDs Context neutral ecosystem Marshaller / Un-marshaller POJO domain model Commands
49. JAX-WS Provider API javax.xml.ws.Provider is a lower-level generic API for processing requests to web service endpoint @WebServiceProvider annotation Enables the endpoint class to work directly with message payloads Allows us to use our own marshalling/un-marshalling component 30
57. Services<wsdl:import> wsdlLocation Provider-based Endpoint Provider-based Endpoint Provider-based Endpoint Provider-based Endpoint Provider-based Endpoint @WebServiceProvider @WebServiceProvider @WebServiceProvider @WebServiceProvider @WebServiceProvider << Abstract >> Endpoint Support Class Each Provider object to extend endpoint support base class 31
58.
59.
60. WSDL Design (1/3) Leverage the document/literal “wrapped” convention Semantically very similar to RPC/literal Internal dispatching/processing of requests is simplified Root element of message payload indicates the operation / command Entire message payload can be schema validated Not so with RPC/literal Not without its disadvantages Requires trickery within the WSDL 34
86. Relationship between canonicals, marshaller, domain model (3/4) 45 Web Services Messaging EJB EDI … XML message Canonical XSDs Marshaller / Un-marshaller Commands POJO domain model
97. Drools is currently a suite of systems:Drools Expert – rule engine Rule language Domain specific languages Rules and decision tables ReteOO – enhanced version of Rete for OO systems Drools Guvnor – business rule management system Web-based Rule authoring and storage Rule management Rule deployment Drools Flow – process and workflow system Others…
100. Connector component provides layer of abstractionExposed as a Java interface Convenient; easy to work with Implementations can be changed/swapped-out without impacting callers; implementation options: JSR-94 Vendor-specific API other
101. Component: Rule Engine Connector (2/4) JSR-94 concepts: Rule Engine Environment to house and execute rules Rule Session Stateless or stateful Associated with a single rule execution set Rule Execution Set Collection of rules Rule If/Then Pluggable provider 51
102. Component: Rule Engine Connector (3/4) 52 // load the provider class... Class.forName("org.drools.jsr94.rules.RuleServiceProviderImpl“); // obtain provider object... RuleServiceProvider provider = RuleServiceProviderManager.getRuleServiceProvider(“http://drools.org”); // create rule administrator object (needed to create execution set)... RuleAdministrator ruleAdmin = provider.getRuleAdministrator(); // create local rule execution set provider... LocalRuleExecutionSetProvider ruleExecSetProvider = ruleAdministrator.getLocalRuleExecutionSetProvider(null); // create the rule execution set... RuleExecutionSet executionSet = ruleExecSetProvider.createRuleExecutionSet( new InputStreamReader(new FileInputStream(“/BusinessRuleDefs.drl”)), null); // register the rule execution set with the rule administrator... ruleAdmin.registerRuleExecutionSet(executionSet.getName(), executionSet, null); // get handle to rule runtime... RuleRuntime ruleRuntime = provider.getRuleRuntime(); // create stateless rule session... StatelessRuleSession session = ruleRuntime.createRuleSession(executionSet.getName(), null, RuleRuntime.STATELESS_SESSION_TYPE); // execute the rules and get back the results... List results = session.executeRules(inputObjectsList);
104. Component: Order Rule Access Object (RAO) (1/2) Similar in concept to DAO pattern Clients will interact with rule engine via this component; not the BREConnector Just as a client would interface with a DAO interface and not some O/R or JDBC-based integration component Provides domain-specific interface to access the rule engine 54
110. Apache jUDDI and Apache Scout Demo application leverages Apache jUDDI and Apache Scout Apache jUDDI UDDI registry Compliant with UDDI 3 specification (level-0) Provides web service and Java-based API to query and manage the registry Apache Scout JAXR implementation UDDI version 2 and 3 compliant 58
111. Publishing entries to UDDI Registry (1/3) Leveraging soapUI to access jUDDI web service-based API Publishing a business: 59
116. Connector component provides layer of abstractionExposed as a Java interface Convenient; easy to work with Implementations can be changed/swapped-out without impacting callers; implementation options: JAXR Vendor-specific API other
119. Component: Commands Abstraction (1/2) Inbound request is un-marshalled into a command object Command executor executes command Business logic encapsulated within command Clean programming model New command objects can be created to accommodate new <portType> operations No need to modify JAX-WS SEI; just create new command classes Similar to “EJB Command” design pattern 66
121. Component: Service Client Business delegate-style object to allow easy access to web service Methods in service client interface match-up to <portType> operations from web service As usual, interface-based Today’s implementation talks to a web service Tomorrow’s implementation could talk to remote EJBs Day after tomorrow’s implementation could be local method calls within the same JVM 68
122. Agenda The Requirements Architecture of Proposed Solution Major Components Demo Summary Q/A 69
123. Agenda The Requirements Architecture of Proposed Solution Major Components Demo Summary Q/A 70
124. Summary Demo app makes very rudimentary usage of Drools Expert No central rule engine server to house and execute rules Rule files are loaded and executed locally (same JVM) within the BREConnector implementation Simple design, but not naïve Design can accommodate changes in requirements and technology Technology usage == implementation details Interface-based design allows solution stacks to be swapped out without affecting callers Not the only way to design a service Architecture and design are informed by requirements; not technology Good architecture flows from loose coupling and sound object-oriented principals 71
125. Agenda The Requirements Architecture of Proposed Solution Major Components Demo Summary Q/A 72
126. QA Thank You for Attending! Please remember to full out the session survey available under the “Survey” tab of the online schedule builder (Session: S313909) The source code for this presentation can be found at: http://bit.ly/blueslate-javaone2010 73
Notas do Editor
The functional requirements call for a service that can compute the sub-total of an order for some books. The sub-totalling should be broken-down by line item and business should be in place for determining things like discounts (such as free shipping).
We also have some non-functional requirements as well. The business rules should be maintainable by tech-savvy business analysts.The service should be designed in such a way that it is componentized
There’s a distinction between the service itself, and the service endpoint. Our service is designed such that multiple endpoints can expose the service, yet, there is still only 1 service implementation “behind” the endpoints. This is something that WSDL supports. A single port type (a grouping of operations) can have multiple bindings. Each binding can then have multiple permutations.We’re going to see how JAX-WS makes it pretty simple to implement these ideas…
In furtherance of this, we divide our WSDL into 2 partitions – the first partition contains the logical definitions: types, messages, port types. The 2nd partition defines the concrete portion of the WSDL – the bindings and services. With this structure, we have a separate concrete WSDL file for each type of endpoint we want to support.
high level Java-centric abstraction that hides the details of converting between Javaobjects and their XML representations for use in XML-based messages
Remember we were talking about the separation of logical service definition and concrete bindings. This pictorial attempts to show the relationship between a Provider-annotated Java class and a concrete WSDL definition. The reality of how the Provider annotation works presents a challenge to us in remaining DRY. Regardless of endpoint, we want to process messages in a uniform way. To accomplish this, we’ll create an abstract ‘endpoint service support’ class that each concrete Provider-annotated class can extend from.
This allows us to place our common processing-logic into a single class, and, each concrete provider class will just inherit the functionality.
Thus, our concrete Provider-annotated service class only really exists to:(+) inherit from our endpoint support classess and(+) to contain the Provider annotation tying it to the associated concrete WSDL definitionNotice the class itself is empty…
So we’ve talked about how to “break-up” the WSDL into “the logical” and “the concrete”, however we haven’t touched upon yet the content of the WSDL; the *design* of our web service from the perspective of its contract.
So we’ve talked about how to “break-up” the WSDL into “the logical” and “the concrete”, however we haven’t touched upon yet the content of the WSDL; the *design* of our web service from the perspective of its contract.
And so here’s the wire format
Again, we just want to stress the notion that there is a pretty tight relationship between our POJO domain model and our canonicals. It’s important to point out that the canonicals only partially help inform the design of the domain model. It is OO principals and the requirements that ultimately drive the design of the domain model. Similarly, it is good hierarchical data-modeling that informs the design and creation of the canonicals. It is the job of the marshaller model to translate XML instances of the canonicals to and from objects from our domain model. The last thing to restate is, we’re not tied to a web service or web or another other sort of computing-context.
Although our implementation is quite naïve, we’re remaining true to our axiom that we should be designing to interfaces, and not technologies. Today our marshaller interface is implemented using JAXB, tomorrow, could be something different…Our design is “future proof” to the extent that our interface is robust.
Notables: eclipse plug-in
So as you can see here JSR-94 is a pretty verbose API, and obviously we want to provide a more convenient API for clients to work with. What’s not shown here is resource cleanup-code surrounded by try/catch/finally, so in reality, the code would even be more cluttered in this in a real system.
So instead of working with JSR-94 directly, clients can use this interface. We’ll have a JSR-94-based implementation that just default to creating a stateless rule session with the rule service.
One method per rule execution set – potential strategy for designing RAOs
As per usual we create a generalized interface that is tied to the domain of A REGISTRY, and not JAXR.
Just trying to show here that like JDBC, JSR-94, etc, JAXR is a relatively verbose API, and thus we have our interface in front of it for clients to interact with.
DSR takes 1st 3 bullets; Paul takes last 2 bullets