Examples: Outbound messaging using SendMsgCmd

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 email message and send it through the outbound messaging system:

Example 1

In this example, you can use either SendMsgCmd.setContent or SendMsgCmd.compose to build a new XML message and send it through the outbound messaging system.
Important: If SendMsgCmd.setContent and SendMsgCmd.compose are used together, content that is generated by composition can override the other according to the following behavior priorities:
  • If SendMsgCmd.setContent is called with the languageId parameter, its generated content is overridden when SendMsgCmd.compose is called.
  • If SendMsgCmd.setContent is called without the languageId parameter, its generated content is not overridden when SendMsgCmd.compose is called.

Read the commented lines carefully and choose a method when you are 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 your 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.

SendMsgCmd.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 SendMsgCmd.setContent(Integer, String, byte[]) and SendMsgCmd.compose(String,CommandContext,TypedProperty) 
// are used, content generated by composition will override the other.
// If SendMsgCmd.setContent is called with the languageId parameter, its generated content is overridden when SendMsgCmd.compose is called.
// If SendMsgCmd.setContent is called without the languageId parameter, its generated content is not overridden when SendMsgCmd.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.

SendMsgCmd.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);

}


com.ibm.commerce.messaging.commands.SendMsgCmd sendMessage = (com.ibm.commerce.messaging.commands.SendMsgCmd) CommandFactory.createCommand(SendMsgCmd.NAME, getStoreId());

		// Set the current storeId.  To use the site configuration, set the storeId to 0.
		sendMessage.setStoreID(getStoreId());
		
		// Set the message type.  Message types are defined in the MSGTYPES table and are associated with a
		// a particular transport in the WC Admin Console.  
		sendMessage.setMsgType("OrderReceived");
		
		// Example - building a message in a String object and using SendMsgCmd.setContent().
		String OrderNotifyMsg = new String("Your Order has been received. Thank You for Shopping with us.");

		// The first parameter is null. This means that the transport used is dependent on the 
		// value set in the Administration Console.
		// This example hardcode's the language to English (-1).		
		sendMessage.setContent(null, "-1", OrderNotifyMsg.getBytes());
		// End Example
		 
		// Example - building the message using the composition service.  The composition service
		// will use a JSP to build and format the message.

		// Populate a TypedProperty object with any additional parameters the composition JSP requires.
		TypedProperty compositionTypedProperty = null;

		// Use the composition service to compose the message.  This will lookup the JSP associated with the 
		// view as defined in the Struts configuration.  The output of the JSP will be used as the message.
		// The first parameter is null, this means the default ViewName defined in the MSGTYPES table is used.
		// For the 'OrderReceived' message, the default ViewName is 'OrderReceivedView'.
		// A custom view may be specified to override the default. 


		sendMessage.compose(null, getCommandContext(), compositionTypedProperty);
		// End Example
		 
		// Set the subject, recipient and sender information using Configurable message data services.		
		sendMessage.setConfigData("subject","Your Order has been received");
		sendMessage.setConfigData("recipient","recipient@recipient.com");
		sendMessage.setConfigData("sender","storeAdmin@storeABC.com");

		// Send out the message using sendImmediate send service.
		sendMessage.sendImmediate();

		// Set the command context obtained from the controller command.
		sendMessage.setCommandContext(getCommandContext());

		// Run the outbound messaging system services
		sendMessage.execute();

Example 2

In this example, you can use either SendMsgCmd.setContent or SendMsgCmd.compose to build an email message and send it through the outbound messaging system.
Important: If SendMsgCmd.setContent and SendMsgCmd.compose are used together, content that is generated by composition can override the other according to the following behavior priorities:
  • If SendMsgCmd.setContent is called with the languageId parameter, its generated content is overridden when SendMsgCmd.compose is called.
  • If SendMsgCmd.setContent is called without the languageId parameter, its generated content is not overridden when SendMsgCmd.compose is called.

Read the commented lines documentation carefully and choose a method when you are 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 your 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.
SendMsgCmd.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.

SendMsgCmd.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);

Composition JSP files

The default view for the OrderReceived message is defined in the MSGTYPES table and is the OrderReceivedView. This view is defined in the struts-config-ext.xml file. This view refers to the OrderReceivedNotify.jsp, which is used to compose the message:

<forward className="com.ibm.commerce.struts.ECActionForward" name="OrderReceivedView/10101/-3" path="/Messages/OrderReceivedNotify.jsp">
<set-property property="implClassName" value="com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl"/>
<set-property property="interfaceName" value="com.ibm.commerce.messaging.viewcommands.MessagingViewCommand"/>
</forward>
See the OrderReceivedNotify.jsp file for details on how the message is composed.