Wednesday, 6 February 2013

Lets SOA Suite UMS do your Application Messaging

 With User Messaging Service you can let Soa Suite 11g do all your Messaging. And UMS can do a lot: like Email with Attachments, Instant Messaging (XMPP, like Google Talk), SMS Text messages (SMPP), Fax or Voice Messages and deliver messages to the Soa & Webcenter Task List. Besides this, UMS keeps track of delivery status information provided by messaging gateways and makes this information available to applications so that they can respond to a failed delivery.
In Patch Set 2 of Soa Suite 11g Oracle added a simple java API for UMS and in this blogpost I will use this API in a EJB Session Bean with a Remote Interface and also expose this Session Bean as a Web Service. This Session Bean & Web Service can be used in your custom applications so you don't need to have or build your own messaging methods because Soa Suite can do more and much better and even keeps track of your messages. Just call the Web Service or the Session Bean, deliver a list of Receivers with names and the media delivery type and it will return a MessageId with this you can check the status of your send messages.

In this blogpost I will use the email and instant messaging driver to deliver my messages. And as a demo I can send email attachements, deliver a message to IM and Email Client. ( Multi part Message , plain text part for IM and HTML part for Email ) and at last get retrieve the status of these messages. And you can use this in SoapUI, a web service proxy or in a EJB java client.

First you need to enable and configure the UMS Email and Instant Messaging Driver ( XMPP).
Go to the Weblogic console of your Soa Domain probably http://xxxxx:7001/console and select the XMPP UMS driver and go the the target tab and enable the Soa servers or soa cluster.


You need to reboot the Soa Server managed Servers.
In the User Messaging Service part of the Enterprise Manager Website http://xxxx:7001/em you can select the xmpp driver and go the XMPP Driver Properties.
In my case I will use Google Talk as IM service. Provide the server and a Talk user account.
Do the Same for your UMS Email driver. Provide the SMTP server.
And enable all the Work Flow communications in the WorkFlow Notifications Properties page of the Soa Infrastructure part..

You need to reboot the Soa Server managed Servers.

Now you can go to the developer part and create a JDeveloper project and add an new Application. Your project needs to have the UMS library located at jdeveloper\communications\modules\oracle.sdp.messaging_11.1.1\sdpmessaging.jar and your application also needs to have a reference to the oracle.sdp.messaging shared library. Do this in the weblogic-application.xml.
This EJB / WS needs to deployed on the Soa Suite Managed Servers.

To make things more simple I made two entities Receiver and Status.
The Receiver just needs an email address and type ( EMAIL or IM )
  1. package nl.whitehorses.soa.ums.entities;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. public class Receiver implements Serializable{  
  6.     public Receiver() {  
  7.   
  8.     }  
  9.   
  10.     private String receiver;  
  11.     private String channel;  
  12.   
  13.     public Receiver(String receiver, String channel) {  
  14.         super();  
  15.         this.receiver = receiver;  
  16.         this.channel = channel;  
  17.     }  
  18.   
  19.     public void setReceiver(String receiver) {  
  20.         this.receiver = receiver;  
  21.     }  
  22.   
  23.     public String getReceiver() {  
  24.         return receiver;  
  25.     }  
  26.   
  27.     public void setChannel(String channel) {  
  28.         this.channel = channel;  
  29.     }  
  30.   
  31.     public String getChannel() {  
  32.         return channel;  
  33.     }  
  34. }  
The Status entity gives some simple info about the send messages.
  1. package nl.whitehorses.soa.ums.entities;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.Date;  
  5.   
  6. public class Status implements Serializable {  
  7.     public Status() {  
  8.     }  
  9.         
  10.     private String messageId;  
  11.     private Date   sendTime;  
  12.     private String receiver;  
  13.     private String status;  
  14.   
  15.     public Status(String messageId, Date sendTime, String receiver,  
  16.                   String status) {  
  17.         super();  
  18.         this.messageId = messageId;  
  19.         this.sendTime = sendTime;  
  20.         this.receiver = receiver;  
  21.         this.status = status;  
  22.     }  
  23.   
  24.     public void setMessageId(String messageId) {  
  25.         this.messageId = messageId;  
  26.     }  
  27.   
  28.     public String getMessageId() {  
  29.         return messageId;  
  30.     }  
  31.   
  32.     public void setSendTime(Date sendTime) {  
  33.         this.sendTime = sendTime;  
  34.     }  
  35.   
  36.     public Date getSendTime() {  
  37.         return sendTime;  
  38.     }  
  39.   
  40.     public void setReceiver(String receiver) {  
  41.         this.receiver = receiver;  
  42.     }  
  43.   
  44.     public String getReceiver() {  
  45.         return receiver;  
  46.     }  
  47.   
  48.     public void setStatus(String status) {  
  49.         this.status = status;  
  50.     }  
  51.   
  52.     public String getStatus() {  
  53.         return status;  
  54.     }  
  55.       
  56.     public String toString() {  
  57.       return "message id: "+ messageId+" time: "+sendTime.toString()+ " status: " +status + " receiver: " + receiver;    
  58.     }  
  59. }  
Here the main code with three method sendMailAttachmentMsg, sendMixedMsg and getMessageStatus.
The sendMailAttachmentMsg method will only send Mail with Attachments and sendMixedMsg method can send messages to Google Talk and Email, it depends on the receivers and getMessageStatus returns the Statusses of the send messages.
  1. package nl.whitehorses.soa.ums.services;  
  2.   
  3.   
  4. import javax.ejb.Remote;  
  5. import javax.ejb.Stateless;  
  6.   
  7. import java.util.HashMap;  
  8. import java.util.Map;  
  9.   
  10. import java.util.ArrayList;  
  11. import java.util.List;  
  12.   
  13. import javax.activation.DataHandler;  
  14.   
  15. import javax.jws.WebMethod;  
  16. import javax.jws.WebParam;  
  17. import javax.jws.WebResult;  
  18. import javax.jws.WebService;  
  19.   
  20. import javax.mail.internet.MimeBodyPart;  
  21. import javax.mail.internet.MimeMultipart;  
  22.   
  23. import nl.whitehorses.soa.ums.entities.Receiver;  
  24. import nl.whitehorses.soa.ums.utilities.ReceiverConvertor;  
  25.   
  26. import oracle.sdp.messaging.Address;  
  27. import oracle.sdp.messaging.ApplicationInfo;  
  28. import oracle.sdp.messaging.DeliveryType;  
  29. import oracle.sdp.messaging.Message;  
  30. import oracle.sdp.messaging.MessagingFactory;  
  31. import oracle.sdp.messaging.MessagingClient;  
  32. import oracle.sdp.messaging.MessagingClientFactory;  
  33. import oracle.sdp.messaging.MessagingException;  
  34. import oracle.sdp.messaging.Status;  
  35. import oracle.sdp.messaging.util.ByteArrayDataSource;  
  36. import oracle.sdp.messaging.util.StringDataSource;  
  37.   
  38. @Stateless(name="SessionEJB", mappedName = "SoaUMSMessaging-SessionEJB")  
  39. @Remote  
  40. @WebService(name        = "UMSMessagingService",   
  41.             serviceName = "UMSMessagingService",  
  42.             portName    = "UMSMessagingServicePort")  
  43. public class SessionEJBBean implements SessionEJB {  
  44.   
  45.     MessagingClient mClient = null;  
  46.     Map<String, Object> params = new HashMap<String, Object>();  
  47.   
  48.     public SessionEJBBean() {  
  49.       params.put(ApplicationInfo.APPLICATION_NAME, "CompanyMsgr");  
  50.       params.put(ApplicationInfo.APPLICATION_INSTANCE_NAME, "CompanyInstance");  
  51.     }  
  52.   
  53.     @WebMethod  
  54.     @WebResult(name = "messageId")  
  55.     public String sendMailAttachmentMsg(  
  56.                                         @WebParam(name = "sender")    String sender,   
  57.                                         @WebParam(name = "receivers") List<Receiver> receivers,  
  58.                                         @WebParam(name = "subject")   String subject,  
  59.                                         @WebParam(name = "message")   String message,  
  60.                                         @WebParam(name = "data"byte[] data,  
  61.                                         @WebParam(name = "mimetype") String mimetype,  
  62.                                         @WebParam(name = "filename") String fileName) {  
  63.   
  64.       MimeMultipart mp = null;  
  65.   
  66.       try {  
  67.           // plain msg for the body  
  68.           MimeBodyPart mp_partPlain = new MimeBodyPart();  
  69.           mp_partPlain.setText(message);  
  70.           
  71.           // binary attachement  
  72.           MimeBodyPart mp_partBinary = new MimeBodyPart();  
  73.           ByteArrayDataSource dataSource = new ByteArrayDataSource(data, mimetype);  
  74.           mp_partBinary.setDataHandler(new DataHandler(dataSource));  
  75.           mp_partBinary.setFileName(fileName);  
  76.   
  77.           mp = new MimeMultipart("alternative");  
  78.           mp.addBodyPart(mp_partPlain);  
  79.           mp.addBodyPart(mp_partBinary);  
  80.         
  81.       } catch (javax.mail.MessagingException e) {  
  82.           e.printStackTrace();  
  83.       }     
  84.   
  85.         try {  
  86.           Message soaMessage = MessagingFactory.createMessage();  
  87.           Address soaSender =  
  88.               MessagingFactory.buildAddress(sender,   
  89.                                             DeliveryType.EMAIL,  
  90.                                             null);  
  91.   
  92.           soaMessage.setSubject(subject);  
  93.           soaMessage.setContent(mp, "multipart/alternative");  
  94.             
  95.           soaMessage.addSender(soaSender);  
  96.           soaMessage.addAllRecipients(ReceiverConvertor.convertReceiver(receivers));  
  97.   
  98.           mClient = MessagingClientFactory.createMessagingClient(params);  
  99.           String messageId = mClient.send(soaMessage);  
  100.           return messageId;  
  101.   
  102.         } catch (MessagingException e) {  
  103.              e.printStackTrace();  
  104.         }  
  105.    
  106.         return null;  
  107.     }  
  108.   
  109.   
  110.     @WebMethod  
  111.     @WebResult(name = "statusses")      
  112.     public List<nl.whitehorses.soa.ums.entities.Status> getMessageStatus(  
  113.             @WebParam(name = "messageId") String messageId) {  
  114.   
  115.         try {  
  116.             mClient = MessagingClientFactory.createMessagingClient(params);  
  117.             Status[] msgStatuses = mClient.getStatus(messageId);  
  118.             List<nl.whitehorses.soa.ums.entities.Status> statusses =  
  119.                 new ArrayList<nl.whitehorses.soa.ums.entities.Status>();  
  120.   
  121.             for (Status msgStatus : msgStatuses) {  
  122.                 nl.whitehorses.soa.ums.entities.Status status =  
  123.                     new nl.whitehorses.soa.ums.entities.Status();  
  124.                 status.setMessageId(msgStatus.getMessageId());  
  125.                 status.setReceiver(msgStatus.getAddress().toString());  
  126.                 status.setStatus(msgStatus.getType().toString());  
  127.                 status.setSendTime(msgStatus.getDate().getTime());  
  128.                 statusses.add(status);  
  129.             }  
  130.             return statusses;  
  131.         } catch (MessagingException e) {  
  132.             e.printStackTrace();  
  133.         }  
  134.         return null;  
  135.     }  
  136.   
  137.     @WebMethod  
  138.     @WebResult(name = "messageId")  
  139.     public String sendMixedMsg(@WebParam(name = "subject") String subject,  
  140.                                @WebParam(name = "message") String message,   
  141.                                @WebParam(name = "sender") String sender,   
  142.                                @WebParam(name = "receivers") List<Receiver> receivers) {  
  143.   
  144.   
  145.         Message soaMessage = MessagingFactory.createMessage();  
  146.         MimeMultipart part_mp = null;  
  147.   
  148.         try {  
  149.           // IM Message    
  150.           MimeBodyPart part_mp_partPlain = new MimeBodyPart();  
  151.           // set IM    
  152.           part_mp_partPlain.addHeader(soaMessage.HEADER_SDPM_PAYLOAD_PART_DELIVERY_TYPE,  
  153.                                       "IM");  
  154.           part_mp_partPlain.setText(message);  
  155.    
  156.           // Email Message               
  157.           MimeBodyPart part_mp_partRich = new MimeBodyPart();  
  158.           // set Email    
  159.           part_mp_partRich.addHeader(soaMessage.HEADER_SDPM_PAYLOAD_PART_DELIVERY_TYPE,  
  160.                                      "EMAIL");  
  161.           StringDataSource htmlDataSource =   
  162.               new StringDataSource("<html><head></head><body><b><i>"+  
  163.                                    message+   
  164.                                    "</i></b></body></html>",  
  165.                                    "text/html; charset=UTF-8");  
  166.           part_mp_partRich.setDataHandler(new DataHandler(htmlDataSource));  
  167.       
  168.           part_mp = new MimeMultipart("alternative");  
  169.           part_mp.addBodyPart(part_mp_partPlain);  
  170.           part_mp.addBodyPart(part_mp_partRich);  
  171.   
  172.         } catch (javax.mail.MessagingException b) {  
  173.               b.printStackTrace();  
  174.         }  
  175.   
  176.   
  177.         try {  
  178.             mClient = MessagingClientFactory.createMessagingClient(params);  
  179.   
  180.             soaMessage.setSubject(subject);  
  181.             soaMessage.setContent(part_mp, "multipart/alternative");  
  182.             // Enabled for IM and Email  
  183.             soaMessage.setMultiplePayload(true);  
  184.   
  185.             Address soaSender = MessagingFactory.buildAddress(sender,  
  186.                                                               DeliveryType.EMAIL,  
  187.                                                               null);  
  188.   
  189.             soaMessage.addSender(soaSender);  
  190.             soaMessage.addAllRecipients(ReceiverConvertor.convertReceiver(receivers));  
  191.   
  192.             String messageId = mClient.send(soaMessage);  
  193.             return messageId;  
  194.         } catch (oracle.sdp.messaging.MessagingException b) {  
  195.             b.printStackTrace();  
  196.         }  
  197.         return null;  
  198.     }  
  199.   
  200. }  

My EJB test client
  1. package nl.whitehorses.soa.ums;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.FileNotFoundException;  
  5.   
  6. import java.io.IOException;  
  7.   
  8. import java.nio.ByteBuffer;  
  9. import java.nio.channels.FileChannel;  
  10.   
  11. import java.util.ArrayList;  
  12. import java.util.Hashtable;  
  13.   
  14. import java.util.List;  
  15.   
  16. import javax.naming.Context;  
  17. import javax.naming.InitialContext;  
  18. import javax.naming.NamingException;  
  19.   
  20. import nl.whitehorses.soa.ums.entities.Receiver;  
  21. import nl.whitehorses.soa.ums.entities.Status;  
  22. import nl.whitehorses.soa.ums.services.SessionEJB;  
  23.   
  24. public class SessionEJBClient {  
  25.     public static void main(String[] args) {  
  26.         try {  
  27.             final Context context = getInitialContext();  
  28.             SessionEJB sessionEJB =  
  29.                 (SessionEJB)context.lookup("SoaUMSMessaging-SessionEJB#nl.whitehorses.soa.ums.services.SessionEJB");  
  30.   
  31.             List<Receiver> receivers = new ArrayList<Receiver>();  
  32.             Receiver receiver  = new Receiver("xxx@gmail.com""EMAIL");  
  33.             Receiver receiver2 = new Receiver("xxxx@tiscali.nl""EMAIL");  
  34.             receivers.add(receiver);  
  35.             receivers.add(receiver2);  
  36.   
  37.   
  38.   
  39.             String messageId =  
  40.                 sessionEJB.sendMailAttachmentMsg( "xxxx@xs4all.nl",  
  41.                                                    receivers,  
  42.                                                    "hello subject",  
  43.                                                    "hello body"  
  44.                                                    ,doDownload("C:/temp/logo.png"),  
  45.                                                    "image/png",  
  46.                                                    "logo.png");  
  47.   
  48.             System.out.println("MessageId is " + messageId +  
  49.                                " lets wait 2 seconds for the status");  
  50.             Thread.sleep(2000);  
  51.   
  52.             if (messageId != null) {  
  53.               List<Status> statusses =  
  54.                   sessionEJB.getMessageStatus(messageId);  
  55.               for (Status status : statusses) {  
  56.                   System.out.println(status.toString());  
  57.               }  
  58.             }  
  59.   
  60.   
  61.             Receiver receiver3 = new Receiver("nl2en@bot.talk.google.com""IM");  
  62.             receivers.add(receiver3);  
  63.   
  64.             messageId = sessionEJB.sendMixedMsg("Hello",  
  65.                                                 "Hello world",  
  66.                                                 "xxxx@xs4all.nl",  
  67.                                                 receivers );  
  68.   
  69.   
  70.             System.out.println("MessageId is " + messageId +  
  71.                                " lets wait for the status");  
  72.             Thread.sleep(4000);  
  73.   
  74.             if (messageId != null) {  
  75.                 List<Status> statusses =  
  76.                     sessionEJB.getMessageStatus(messageId);  
  77.                 for (Status status : statusses) {  
  78.                     System.out.println(status.toString());  
  79.                 }  
  80.             }  
  81.   
  82.         } catch (Exception ex) {  
  83.             ex.printStackTrace();  
  84.         }  
  85.     }  
  86.   
  87.     private static Context getInitialContext() throws NamingException {  
  88.         Hashtable env = new Hashtable();  
  89.         env.put(Context.INITIAL_CONTEXT_FACTORY,  
  90.                 "weblogic.jndi.WLInitialContextFactory");  
  91.         env.put(Context.PROVIDER_URL, "t3://localhost:8001");  
  92.         return new InitialContext(env);  
  93.     }  
  94.   
  95.     public static byte[] doDownload(String fileName) {  
  96.         FileInputStream fis;  
  97.         byte[] data = null;  
  98.         FileChannel fc;  
  99.   
  100.         try {  
  101.             fis = new FileInputStream(fileName);  
  102.             fc = fis.getChannel();  
  103.             data = new byte[(int)(fc.size())];  
  104.             ByteBuffer bb = ByteBuffer.wrap(data);  
  105.             fc.read(bb);  
  106.         } catch (FileNotFoundException e) {  
  107.             // TODO  
  108.         } catch (IOException e) {  
  109.             // TODO  
  110.         }  
  111.         return data;  
  112.     }  
  113. }  
If you send messages with this EJB Session or WS then you can take a look at the application client in the UMS service overview.

Here you can download  Edwin Biemondworkspace

No comments:

Post a Comment