Exemples : Messagerie sortante à l'aide de SendMsgCmd
Le segment de code Java suivant montre comment les interactions avec le système de messagerie sortante peuvent avoir lieu. L'exemple 1 vous montre comment créer un nouveau message XML et l'envoyer via le système de messagerie sortante. L'exemple 2 vous montre comment créer un e-mail et l'envoyer via le système de messagerie sortante :
Exemple 1
Dans cet exemple, vous pouvez utiliser
SendMsgCmd.setContent ou SendMsgCmd.compose pour créer un nouveau message XML et l'envoyer via le système de messagerie sortante.Important : Si
SendMsgCmd.setContent et SendMsgCmd.compose sont utilisés ensemble, le contenu généré par la composition peut remplacer l'autre en fonction des priorités de comportement suivantes :- Si
SendMsgCmd.setContentest appelé avec le paramètre languageId, son contenu généré est remplacé lorsqueSendMsgCmd.composeest appelé. - Si
SendMsgCmd.setContentest appelé sans le paramètre languageId, son contenu généré n'est pas remplacé lorsqueSendMsgCmd.composeest appelé.
Lisez attentivement les lignes commentées et choisissez une méthode lorsque vous copiez l'extrait de code ci-dessous. Il n'est pas recommandé d'utiliser les deux méthodes, car cela ajouterait une complexité inutile.
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();
Exemple 2
Dans cet exemple, vous pouvez utiliser
SendMsgCmd.setContent ou SendMsgCmd.compose pour créer un e-mail et l'envoyer via le système de messagerie sortante.Important : Si
SendMsgCmd.setContent et SendMsgCmd.compose sont utilisés ensemble, le contenu généré par la composition peut remplacer l'autre en fonction des priorités de comportement suivantes :- Si
SendMsgCmd.setContentest appelé avec le paramètre languageId, son contenu généré est remplacé lorsqueSendMsgCmd.composeest appelé. - Si
SendMsgCmd.setContentest appelé sans le paramètre languageId, son contenu généré n'est pas remplacé lorsqueSendMsgCmd.composeest appelé.
Lisez attentivement la documentation des lignes commentées et choisissez une méthode lorsque vous copiez l'extrait de code ci-dessous. Il n'est pas recommandé d'utiliser les deux méthodes, car cela ajouterait une complexité inutile.
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 des fichiers JSP
L'affichage par défaut du message OrderReceived est défini dans le tableau MSGTYPES et correspond à OrderReceivedView. Cette vue est définie dans le fichier de configuration struts, struts-config-ext.xml pour les utilisateurs de la version 9.0.0.x du HCL Commerce et pour les utilisateurs de la version 9.0.1.x de struts-wcs-stores-custom.xml. Cette vue fait référence à OrderReceivedNotify.jsp, qui est utilisé pour composer le message. Pour les utilisateurs de la version 9.0.0.x de HCL Commerce, le message est le suivant :
<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>
Pour les utilisateurs de la version 9.0.1.x, le message est :<result name="OrderReceivedView/10101/-3" type="wcsstore">
<param name="implClassName">com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl</param>
<param name="interfaceName">com.ibm.commerce.messaging.viewcommands.MessagingViewCommand</param>
<param name="location">/EmailTemplates/Order/OrderCreateNotify.jsp</param>
</result>Consultez le fichier OrderReceivedNotify.jsp pour plus de détails sur la composition du message.