Dana Stevens

Ruminations of a software developer.
posts - 8, comments - 370, trackbacks - 0

Drools on Windows (Part 3 of 22) - Object Interop

In order to use the (amazingly cool) Drools rules engine in a .NET world, you've got to figure out how to pass your .NET business object over there to the land of Java.  There are a couple of ways to do this.  I wanted a straightforward way that didn't rely on any 3rd party tools.  Here's the gist of what we're trying to accomplish:

  • Instantiate a .NET object; populate with values.
  • Serialize the object and send it to a queue.
  • Java application monitors the queue; picks up the message text.
  • Java app unmarshals (i.e. deserializes) the text into a Java object.
  • Java app fires the rules, previously loaded, which may or may not modify the Java object's properties.
  • Java app marshals (i.e. serializes) the resulting object, and sends it to a queue.
  • .NET queue monitor picks up the message, deserializes it into a .NET object.
  • Excellent!

To achieve this type of scenario just takes a bit of setup with the objects you intend to pass back and forth.  Here's one way how to do that setup.

A.  Create your .NET object.

Create a class library in .NET.  Here's an example of something we can tinker with for the duration of this blog series.  Compile it into a dll.

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:  using System.Xml.Serialization;
   6:   
   7:  namespace ZenDoodle.Common.Messaging.Models
   8:  {
   9:      [XmlRoot(Namespace = "http://zendoodle.com/messaging/XML-Schema/2009",
  10:               ElementName = "Customer")]
  11:      public class Customer
  12:      {
  13:          public string Name { get; set; }
  14:          public string Address { get; set; }
  15:          public string City { get; set; }
  16:          public string State { get; set; }
  17:          public string PostalCode { get; set; }
  18:          public CustomerType TypeOfCustomer { get; set; }
  19:          public List<string> Messages { get; set; }
  20:   
  21:          public enum CustomerType
  22:          {
  23:              Bronze,
  24:              Silver,
  25:              Gold,
  26:              Platinum,
  27:              Adamantium
  28:          }
  29:      }
  30:  }

 

B.  Get your Xsd on.

Use the Microsoft-provided xsd.exe utility to create your Xsd file. Or hey, roll your own if that's your thing. Whatever.

Sample command:

"C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\x64\xsd.exe" C:\Zen\Zendoodle.dll /o:C:\Zen\Schemas

   1:  <?xml version="1.0" encoding="utf-8"?>
   2:  <xs:schema xmlns:tns="http://zendoodle.com/messaging/XML-Schema/2009" elementFormDefault="qualified" 
targetNamespace="http://zendoodle.com/messaging/XML-Schema/2009" xmlns:xs="http://www.w3.org/2001/XMLSchema">
   3:    <xs:element name="Customer" nillable="true" type="tns:Customer" />
   4:    <xs:complexType name="Customer">
   5:      <xs:sequence>
   6:        <xs:element minOccurs="0" maxOccurs="1" name="Name" type="xs:string" />
   7:        <xs:element minOccurs="0" maxOccurs="1" name="Address" type="xs:string" />
   8:        <xs:element minOccurs="0" maxOccurs="1" name="City" type="xs:string" />
   9:        <xs:element minOccurs="0" maxOccurs="1" name="State" type="xs:string" />
  10:        <xs:element minOccurs="0" maxOccurs="1" name="PostalCode" type="xs:string" />
  11:        <xs:element minOccurs="1" maxOccurs="1" name="TypeOfCustomer" type="tns:CustomerType" />
  12:        <xs:element minOccurs="0" maxOccurs="1" name="Messages" type="tns:ArrayOfString" />
  13:      </xs:sequence>
  14:    </xs:complexType>
  15:    <xs:simpleType name="CustomerType">
  16:      <xs:restriction base="xs:string">
  17:        <xs:enumeration value="Bronze" />
  18:        <xs:enumeration value="Silver" />
  19:        <xs:enumeration value="Gold" />
  20:        <xs:enumeration value="Platinum" />
  21:        <xs:enumeration value="Adamantium" />
  22:      </xs:restriction>
  23:    </xs:simpleType>
  24:    <xs:complexType name="ArrayOfString">
  25:      <xs:sequence>
  26:        <xs:element minOccurs="0" maxOccurs="unbounded" name="string" nillable="true" type="xs:string" />
  27:      </xs:sequence>
  28:    </xs:complexType>
  29:  </xs:schema>

 

C.  From Xsd to .java - Into the Breach!

The Java utility for working with schemas is xjc.exe.  Actually, as with all things Java, there are at least 835,234 different ways of doing this.  This is just how I do it.  YMMV.

Sample command:

"C:\Program Files (x86)\Java\jdk1.6.0_12\bin\xjc.exe" -verbose C:\Zen\Schemas\schema0.xsd -p com.zendoodle.customer

This will generate .java source code classes.  Which is great, but you must make one modification! And that is to identify the XmlRootElement by name.  I've highlighted the two lines of code that you have to add below in bold red.

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.3 in JDK 1.6
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2009.05.04 at 08:59:01 PM EDT
//


package com.zendoodle.customer;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * <p>Java class for Customer complex type.
 *
 * <p>The following schema fragment specifies the expected content contained within this class.
 *
 * <pre>
 * &lt;complexType name="Customer">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="Name" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *         &lt;element name="Address" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *         &lt;element name="City" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *         &lt;element name="State" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *         &lt;element name="PostalCode" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *         &lt;element name="TypeOfCustomer" type="{http://zendoodle.com/messaging/XML-Schema/2009}CustomerType"/>
 *         &lt;element name="Messages" type="{http://zendoodle.com/messaging/XML-Schema/2009}ArrayOfString" minOccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 *
 *
 */
@XmlRootElement(name="Customer")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Customer", propOrder = {
    "name",
    "address",
    "city",
    "state",
    "postalCode",
    "typeOfCustomer",
    "messages"
})
public class Customer {

    @XmlElement(name = "Name")
    protected String name;
    @XmlElement(name = "Address")
    protected String address;
    @XmlElement(name = "City")
    protected String city;
    @XmlElement(name = "State")
    protected String state;
    @XmlElement(name = "PostalCode")
    protected String postalCode;
    @XmlElement(name = "TypeOfCustomer", required = true)
    protected CustomerType typeOfCustomer;
    @XmlElement(name = "Messages")
    protected ArrayOfString messages;

    /**
     * Gets the value of the name property.
     *
     * @return
     *     possible object is
     *     {@link String }
     *    
     */
    public String getName() {
        return name;
    }

    /**
     * Sets the value of the name property.
     *
     * @param value
     *     allowed object is
     *     {@link String }
     *    
     */
    public void setName(String value) {
        this.name = value;
    }

D.  Compile/JAR.

Last thing you have to do for the setup is to compile the .java classes into .class files, and then pack them into a .jar file.  This is so that your set of .class files can be easily referenced.  Include the .jar file in the Build Path of the Java application.

Sample command:

"C:\Program Files (x86)\Java\jdk1.6.0_12\bin\javac.exe" com\zendoodle\customer\*.java

"C:\Program Files (x86)\Java\jdk1.6.0_12\bin\jar.exe" cfv customer.jar com\zendoodle\customer\*.class

Pause

 

 After that, you should be able to move objects with their data back and forth between .NET and Java.  And for crying out oud, if you're ever going to do this more than once, automate it! 

Next time we'll take a look at the queue, and how all the pieces will fit together.  If you love weird and nifty stuff, see you then!

 

Print | posted on Tuesday, May 05, 2009 7:13 PM | Filed Under [ Drools Java ]

Feedback

Gravatar

# re: Drools on Windows (Part 3 of 22) - Object Interop

what about Drools.NET?
5/9/2009 3:30 AM | Eber Irigoyen
Gravatar

# re: Drools on Windows (Part 3 of 22) - Object Interop

Javascript functions are often quite similar, some functions may be missing or addressed differently. The Javascript implementations should be as compliant with the PHP versions as possible, a good indication is that the PHP function manual could also apply to the Javascript version.
1/20/2010 6:45 AM | meilleur guide de casinos en lig
Gravatar

# re: Drools on Windows (Part 3 of 22) - Object Interop

Are you planning to create 4 to 22?
5/6/2010 12:20 PM | Ron
Gravatar

# re: Drools on Windows (Part 3 of 22) - Object Interop

Starting with an introduction to the basic syntax that is essential for writing rules, the book will guide you through validation and human-readable rules that define, maintain, and support your business agility. As a developer, you will be expected to represent policies, procedures and. constraints regarding how an enterprise conducts its business; this book makes it easier by showing you it can be done.
9/8/2010 4:12 AM | online casino
Gravatar

# re: Drools on Windows (Part 3 of 22) - Object Interop

Starting with an introduction to the basic syntax that is essential for writing rules, the book will guide you through validation and human-readable rules that define, maintain, and support your business agility. As a developer, you will be expected to represent policies, procedures and. constraints regarding how an enterprise conducts its business; this book makes it easier by showing you it can be done......
9/8/2010 4:14 AM | online casino
Gravatar

# ugg outlet

Happiness is not about being immortal nor having food or rights in one's hand. It’s about having each tiny wish come true, or having something to eat when you are hungry or having someone's love when you need love.
9/25/2011 9:30 PM | ugg outlet
Gravatar

# re: Drools on Windows (Part 3 of 22) - Object Interop

lingerie
sexy sexy lingerie
sexy lingerie
sexy underwear
lingerie sexy

Shox pas cher
Basket Nike Pas Cher
Air Max pas cher

Nike Air Max 90 Pas cher
Nike pas cher
Air Max 90 pas cher
Chaussures Nike pas cher

iphone 4 case
iphone case
iphone 4 cases
iphone cases

cheap wedding dresses
cheap wedding dress
cheap party dresses
9/30/2011 4:14 AM | sexy sexy lingerie
Gravatar

# re: Drools on Windows (Part 3 of 22) - Object Interop

This is the perfect article. I know more about book,healthy after reading your blog. I blog once you writing new blog.
10/5/2011 11:08 PM | cheap uggs
Gravatar

# re: Drools on Windows (Part 3 of 22) - Object Interop

Drools Fusion offers the Sliding Window operation and it gives us the possibility to catch events of interest as the ones belonging to a window that is constantly moving. The information available here is really very great thanks for sharing with us. Obese Ottawa man listed ad news on Kijiji
10/7/2011 3:28 AM | Anna
Gravatar

# cheapest replica watches

Many people think of the perfect replica rolex watches to purchase for their loved ones. Even if you shop for your special occasion at the last moment, you are sure to find something nice at cartier replica watches.
10/28/2011 10:27 PM | replica watches
Gravatar

# re: Drools on Windows (Part 3 of 22) - Object Interop

Adequately Revealed: It's really an art to prevent new customers with your beautiful writing layout. Genuinely it is outstanding and great information. Thanks a lot for sharing.
12/11/2011 12:03 AM | Dissertation Writing Help
Gravatar

# Noel Vis

愛のエプロン 普段着 着物 飲食店 制服
1/10/2012 2:31 AM | 着物 モダン
Gravatar

# Vikki Bartolome

振袖 レンタル 格安
1/10/2012 2:32 AM | 制服 かわいい
Gravatar

# Lic

replica watches
1/10/2012 2:33 AM | cheap wedding dresses
Gravatar

# Lic

undefined
1/10/2012 2:33 AM | cheap wedding dresses
Gravatar

# Chas Juback

0
1/10/2012 2:35 AM | 着物 大阪
Gravatar

# Sanford Weingard

thanks for sharing
1/10/2012 2:37 AM | 制服屋
Gravatar

# Buddy Barriner

Ilvy25XOCSACm4
1/10/2012 2:39 AM | 学校制服
Gravatar

# Aldo Calixte

rLHrDJJSZ4G
1/10/2012 2:39 AM | 振袖 レンタル 京都
Gravatar

# Kory Wilkening

rDCVs1AcC5N
1/10/2012 2:41 AM | 振袖 着付け 料金
Gravatar

# Lic

dradf
1/10/2012 2:41 AM | cheap wedding dresses
Gravatar

# Writing


Nice Blog,I Like For Putting This Together! "This is obviously one Nice post. Thanks for the valuable information and insights you have so provided here. Keep it up!"
1/24/2012 1:37 AM | Writing Help
Gravatar

# re: Drools on Windows (Part 3 of 22) - Object Interop

cheap nfl kids jersey
Fitzgerald: "I think as an athlete, you're so used to having things a certain way. In the regular season, meetings start at 8 a.m. You break for 30 minutes to have breakfast. The NFL season is like Groundhog's Day. nfl kids jersey
You have a routine. I eat a bagel, some fruit and a smoothie everyday. That's my routine. But the Super Bowl breaks that up. You're in a hotel. You're in ballrooms for meetings. You have fans all around the place. It breaks up the routine, so that's a challenge."
Thursday/Fridaywholesale kids jersey
By late in the week, comfort is setting in. But so is impatience. While the parties are starting up on the peripheral, players begin to spend more time around the hotel lobby talking to their families or teammates. Strict curfews are imposed for each team. nfl kids jersey sale
Ravens linebacker Ray Lewis "The most memorable part of the week? The conversations that I had with (former Ravens teammates) Rod Woodson and Shannon Sharpe. wholesale nfl kids jersey
2/2/2012 2:14 AM | wholesale nfl kids jersey
Gravatar

# casino planet

nice work on this blog......................
3/6/2012 4:28 AM | Rama
Gravatar

# casino planet

Thanks for the valuable information. great blog.
3/6/2012 4:29 AM | Rama
Gravatar

# casino planet

good work on this site.........
3/6/2012 4:30 AM | Rama
Gravatar

# re: Drools on Windows (Part 3 of 22) - Object Interop

The blog is written in such a way that it is so easy to read and understand.
3/6/2012 4:59 AM | domain service
Gravatar

# re: Drools on Windows (Part 3 of 22) - Object Interop

Truth is beautiful. Without doubt, and so are lies
.ADS1
3/20/2012 4:15 AM | oakley outlet
Gravatar

# re: Drools on Windows (Part 3 of 22) - Object Interop

Truth is beautiful. Without doubt, and so are lies
.ADS1WEAAA
3/20/2012 4:16 AM | oakley outlet
Gravatar

# bari jay bridesmaid dress 433 budget dresses bj 433 bjv

bari jay bridesmaid dress 433 budget dresses bj 433 bjv be sure to make
2/16/2013 3:23 AM | dresseswd
Gravatar

# nice

Thanks for sharing
1/3/2014 12:17 PM | LCD BACK COVER

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 6 and 2 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET