Develop > Controller layer > Integrate with back-end systems and external applications > Outbound services > Create an outbound message


Examples: Outbound messaging system interface

The following Java code segment shows how interactions with the outbound messaging system can take place. Example 1 shows you how to build a new XML message and send it through the outbound messaging system. Example 2 shows you how to build an e-mail message and send it through the outbound messaging system:


Example 1

In this example, you can use either api.setContent or api.compose to build a new XML message and send it through the outbound messaging system.

If both api.setContent and api.compose are used, content generated by composition can override the other according to the following behavior priorities:

Read the commented lines documentation carefully and choose a method accordingly when copying the code snippet below. It is not recommended to use both methods, as it adds unnecessary complexity.

try

{

com.ibm.commerce.messaging.commands.SendMsgCmd api =

(com.ibm.commerce.messaging.commands.SendMsgCmd)

CommandFactory.createCommand(SendMsgCmd.NAME, getStoreId()); 
// Assume you have set the msgType in the MSGTYPES table to 100 and you are 
using

// storeId of 10001.

api.setMsgType("OrderCreateXMLFormat");

api.setStoreID(new Integer(10001)); 
 

// You have two choices on how to build the msg: 
// First choice: build the XML msg in a String object and then use the 
setContent().

String CompanyAOrderCreateMsg = new String("<?xml version="1.0" encoding="UTF-8"?> 
...");

// Set the content for English (with language id of -1)

// The first parameter is null. This means that the transport used is 
// dependent on the value set in the Administration Console.

api.setContent(null, "-1" , OrderCreateMsg.getBytes()); 
 

// Or, use the message composition services (compose()) by passing the 
template/view name

// This view, "CompanyAOrderCreateMsgView", name should have been newly 
// created in Struts 
configuration files referring 

// to a JSP message layout template, which requires values set into variables, 
// ORDER_REF_NUMBER and LANGUAGE_ID.

// In this case, the default view, OrderCreateXMLFormatView (associated with msgtype name 
// "OrderCreateXMLFormat") will not be used.

// If both api.setContent(Integer, String, byte[]) and api.compose(String,CommandContext,TypedProperty) 
// are used, content generated by composition will override the other.
// If api.setContent is called with the languageId parameter, its generated content is overridden when api.compose is called.
// If api.setContent is called without the languageId parameter, its generated content is not overridden when api.compose is called.

// If no view can be found under the store id found in the commandContext object, the messaging system 
// will attempt to look up the view with storeent_id of "0".

String viewName = "CompanyAOrderCreateMsgView";

TypedProperty tp = new TypedProperty(); 
// get the orderRefNumber and put it into tp

tp.put("ORDER_REF_NUMBER", getOrderRn().toString()); 
// get the languageId and put it into tp

tp.put("LANGUAGE_ID", getCommandContext().getLanguageId()); 
// Pass the viewName, command Context and parameters stored in tp to compose 
services.

// Upon successful completion, a message is build according to message layout 
defined in the

// JSP message layout template referred by viewName.

api.compose(viewName, getCommandContext(), tp); 
 

// Send out the message using sendTransacted send service.

api.sendTransacted(); 
// Set the command context obtained from the controller command.

api.setCommandContext(getCommandContext()); 
// Run the outbound messaging system services

api.execute(); 
}

catch (Exception ex )

{

ex.printStackTrace(System.err); 
}


Example 2

In this example, you can use either api.setContent or api.compose to build an e-mail message and send it through the outbound messaging system.

If both api.setContent and api.compose are used, content generated by composition can override the other according to the following behavior priorities:

Read the commented lines documentation carefully and choose a method accordingly when copying the code snippet below. It is not recommended to use both methods, as it adds unnecessary complexity.

try

{

com.ibm.commerce.messaging.commands.SendMsgCmd api =

(com.ibm.commerce.messaging.commands.SendMsgCmd)

CommandFactory.createCommand(SendMsgCmd.NAME, getStoreId()); 
// Assume you have set the msgType in the MSGTYPES table to 200 and you are 
using

// storeId of 1.

api.setMsgType("OrderReceived");

api.setStoreID(new Integer(1)); 
 

// You have to choice how to build the msg: 
// First choice: build the XML msg in a String object and then use the 
setContent().

String OrderNotifyMsg =

new String("Your Order has been received. Thank You for Shopping with us."); 
// Set the content for English (with language id of -1)

// The first parameter is null. This means that the transport used is dependent on the 
// value set in the Administration Console.
api.setContent(null, "-1" , OrderCreateMsg.getBytes()); 
 

// Or, use the message composition services (compose()) by passing the 
template/view name

TypedProperty tp = null; 
// Pass the viewName, command Context and null parameter stored in tp to composition 
// services, assuming the JSP file associating with default view does not require 
// any additional values from this command.

// Upon successful completion, a message is build according to message layout 
defined in the

// JSP message layout template referred by viewName associated with the 
// message type OrderReceive.

api.compose(null, getCommandContext(), tp);

  // Set the subject, recipient and sender information using Configurable message data services.
// To adapt the following example to use the file adapter instead of the e-mail adapter,  replace 
// the 3 lines of code for the e-mail adapter with the following 2 lines: // api.setConfigData("location","c:\"); 
// api.setConfigData("FileName","abc.txt"); 

api.setConfigData("subject","Your Order has been received"); 
api.setConfigData("recipient",getEmailAddress()); 
api.setConfigData("sender","storeAdmin@storeABC.com); 
// Send out the message using sendImmediate send service.

api.sendImmediate(); 
// Set the command context obtained from the controller command.

api.setCommandContext(getCommandContext()); 
// Run the outbound messaging system services

api.execute(); 
}

catch (Exception ex )

{

ex.printStackTrace(System.err);


Related concepts

Outbound messaging system

Order notification


Related tasks

Create an outbound message


+

Search Tips   |   Advanced Search