Tuesday, August 02, 2011

Raising Business Event from AMImpl

ADFbc EO gives a declarative way of raising Business Event.But in cases, when you need the Business Events to be raised from somewhere else than EO, how will you do.

Libraries needed

  • Add SOA Runtime Lib to the JPR.
  • Make sure, you have "oracle.soa.workflow.wc" lib deployed and targeted to DefaultServer in your WLS.
  • Add below Lib Reference to the weblogic-application.xml file
<library-ref>
<library-name>oracle.soa.workflow.wc</library-name>
</library-ref>




DataSource Configuration

Business Event internally uses Oracle AQ.It needs the "ENDSource" or "EDNDataSource" to be configured in WLS.

Diagnostic messages which comes from Business Event classes are as below(If you dont setup the Above Data Source)

INFO: Looking for BusinessEventConnectionFactory
INFO: Looking for EDN-DB JNDI configuration to create SAQRemoteBusinessEventConnectionFactory.
INFO: Unable to create SAQRemoteBusinessEventConnectionFactory: [jdbc/EDNSource or jdbc/EDNDataSource] undefined.
INFO: Looking for EDN-JMS JNDI configuration to create JMSRemoteBusinessEventConnectionFactory.
INFO: Unable to create JMSRemoteBusinessEventConnectionFactory: [java:comp/UserTransaction] undefined.
INFO: Unable to create JMSRemoteBusinessEventConnectionFactory: [jms/fabric/EDNConnectionFactory] undefined.
INFO: Failed to get ConnectionFactory instance.

Verifying the PayLoad gets Saved to the DB after you publish the event

Once you publish the event, the data goes into the AQ table called "AQ$EDN_EVENT_QUEUE_TABLE" which is inside schema "soa_infra/soa_infra".

  • From ADF App, raise a Business Event
  • connect to your DB as soa_infra/soa_infra
  • select * from AQ$EDN_EVENT_QUEUE_TABLE
  • You can see the row gets populated
  • AQ$EDN_EVENT_QUEUE_TABLE.USER_DATA EDN_EVENT_DATA()
    desc EDN_EVENT_DATA
    user type definition
    -----------------------------------
    type edn_event_data as object (
    event edn_business_event,
    publish_impl char,
    subject_info varchar2(256),
    target varchar2(1024));

Sample Java Code

import java.util.Iterator;
import java.util.List;

import javax.xml.namespace.QName;

import oracle.fabric.blocks.event.BusinessEventConnection;
import oracle.fabric.blocks.event.BusinessEventConnectionFactory;

import oracle.fabric.blocks.event.BusinessEventSubscriptionManager;

import oracle.fabric.common.BusinessEvent;

import oracle.integration.platform.blocks.event.BusinessEventBuilder;
import oracle.integration.platform.blocks.event.BusinessEventConnectionFactorySupport;

import oracle.integration.platform.blocks.event.EDNFacadeImpl;
import oracle.integration.platform.blocks.event.SubscriptionInfo;

import oracle.soa.management.facade.LocatorFactory;
import oracle.soa.management.internal.facade.edn.Event;
import oracle.soa.management.internal.facade.edn.Subscription;

import oracle.xml.parser.v2.XMLDocument;

import org.w3c.dom.Element;

    private static final String eventName = MyNotification;
private static final String eventNamespace = "http://schemas.oracle.com/events/edl/EventDefinition";
private static final String schemaNamespace = "http://xmlns.oracle.com/events/edl/EventDefinition";
public void raiseBusinessEvent() {
try {
// Get event connection, make sure the following entry exists in weblogic-application.xml
/*
*
*
oracle.soa.workflow.wc

*/
BusinessEventConnectionFactory cf =
BusinessEventConnectionFactorySupport.findRelevantBusinessEventConnectionFactory(true);

if (cf != null) {
BusinessEventConnection conn =
cf.createBusinessEventConnection();

// Build event
BusinessEventBuilder builder =
BusinessEventBuilder.newInstance();

// Specify the event name and namespace. In this prototype, they are constants, eventNamespace, eventName
builder.setEventName(new QName(eventNamespace, eventName));

// Specify the event payload. In this prototype, the getXMLPayload custom method constructs the payload

builder.setBody(getXMLPayload().getDocumentElement());
BusinessEvent event = builder.createEvent();

// Publish event
conn.publishEvent(event, 5);
conn.close();

// Event was sent sucessfully;
} else {
// For debug only
//cf is null
}
} catch (Exception exp) {
// For debug only
// Failed sending event: " + exp.getMessage();
exp.printStackTrace();
}
}

private XMLDocument getXMLPayload() {
Element masterElem, childElem1, childElem2, childElem3, childElem4, childElem5;
XMLDocument document = new XMLDocument();

String userName = "row.getUsername()";

masterElem =
document.createElementNS(schemaNamespace, "MyNotification");
document.appendChild(masterElem);

childElem1 = document.createElementNS(schemaNamespace, "userName");
childElem1.appendChild(document.createTextNode(userName));
masterElem.appendChild(childElem1);

childElem2 =
document.createElementNS(schemaNamespace, "distBudgetIssueVal");

childElem2.appendChild(document.createTextNode("getPublishRecallVal"));
masterElem.appendChild(childElem2);

childElem3 = document.createElementNS(schemaNamespace, "comment");

childElem3.appendChild(document.createTextNode("getNotificationComment()"));
masterElem.appendChild(childElem3);

childElem4 =
document.createElementNS(schemaNamespace, "moduleIdentifier");

childElem4.appendChild(document.createTextNode("BudgetNotification"));
masterElem.appendChild(childElem4);

return document;
}