[Tutorial] Java - JSMPP

My first project (until now) was always involved into messaging system which heavily rely on performance since it is a realt time system. It can accept and send to various implementation on protocol and message format. I will show you simple tutorial of JSMPP, a wrapper library for SMPP API;

For SMPP needs, we use JSMPP as core library,


JSMPP is a java implementation (SMPP API) of SMPP protocol (currently support SMPP v3.4). It provides interfaces to communicate with Message Center or ESME (External Short Message Entity) and able to handle traffic 3000-5000 messages per second.
SMPP is stand for Short Message Peer to Peer. It is a standard protocol for exchanging SMS messages between SMS entities over TCP/IP or X.25 connections.
This API created to reduce the verbosity of the existing SMPP API. It's very simple and easy to use because it hides the complexity of the low level protocol communication such as automatically enquire link request-response.


This post show very simple example of Java class (A message listener, message receiver and message sender)

Below is code of simple sender for our program

 import java.io.IOException;  
 import java.util.Date;  
 import org.jsmpp.InvalidResponseException;  
 import org.jsmpp.PDUException;  
 import org.jsmpp.bean.BindType;  
 import org.jsmpp.bean.DataCodings;  
 import org.jsmpp.bean.ESMClass;  
 import org.jsmpp.bean.NumberingPlanIndicator;  
 import org.jsmpp.bean.RegisteredDelivery;  
 import org.jsmpp.bean.SMSCDeliveryReceipt;  
 import org.jsmpp.bean.TypeOfNumber;  
 import org.jsmpp.extra.NegativeResponseException;  
 import org.jsmpp.extra.ResponseTimeoutException;  
 import org.jsmpp.session.BindParameter;  
 import org.jsmpp.session.SMPPSession;  
 import org.jsmpp.util.AbsoluteTimeFormatter;  
 import org.jsmpp.util.TimeFormatter;  
 public class SimpleSubmitRegisteredExample {  
 private static TimeFormatter timeFormatter = new AbsoluteTimeFormatter();;  
   public static void main(String[] args) {  
     SMPPSession session = new SMPPSession();  
     // Set listener to receive deliver_sm  
     session.setMessageReceiverListener(new MessageReceiverListenerImpl());  
     try {  
       session.connectAndBind("localhost", 8056, new BindParameter(BindType.BIND_TRX, "test", "test", "cp", TypeOfNumber.UNKNOWN, NumberingPlanIndicator.UNKNOWN, null));  
     } catch (IOException e) {  
       System.err.println("Failed connect and bind to host");  
       e.printStackTrace();  
     }  
     try {  
       String messageId = session.submitShortMessage("CMT", TypeOfNumber.INTERNATIONAL, NumberingPlanIndicator.UNKNOWN, "1616", TypeOfNumber.INTERNATIONAL, NumberingPlanIndicator.UNKNOWN, "628176504657", new ESMClass(), (byte)0, (byte)1, timeFormatter.format(new Date()), null, new RegisteredDelivery(SMSCDeliveryReceipt.SUCCESS_FAILURE), (byte)0, DataCodings.ZERO, (byte)0, "jSMPP simplify SMPP on Java platform".getBytes());  
       /*  
        * you can save the submitted message to database.  
        */  
       System.out.println("Message submitted, message_id is " + messageId);  
       Thread.sleep(2000);  
     } catch (PDUException e) {  
       // Invalid PDU parameter  
       System.err.println("Invalid PDU parameter");  
       e.printStackTrace();  
     } catch (ResponseTimeoutException e) {  
       // Response timeout  
       System.err.println("Response timeout");  
       e.printStackTrace();  
     } catch (InvalidResponseException e) {  
       // Invalid response  
       System.err.println("Receive invalid respose");  
       e.printStackTrace();  
     } catch (NegativeResponseException e) {  
       // Receiving negative response (non-zero command_status)  
       System.err.println("Receive negative response");  
       e.printStackTrace();  
     } catch (IOException e) {  
       System.err.println("IO error occur");  
       e.printStackTrace();  
     } catch (InterruptedException e) {  
       System.err.println("Thread interrupted");  
       e.printStackTrace();  
     }  
     session.unbindAndClose();  
   }  
 }  

Basically we set a message listener into SMPPSession instance and then attempt to connect and bind to defined parameters, after that it will try to submit a short message.

Ok now we need to make the listener class, simply copy paste the code below

 import org.jsmpp.bean.AlertNotification;  
 import org.jsmpp.bean.DataSm;  
 import org.jsmpp.bean.DeliverSm;  
 import org.jsmpp.bean.DeliveryReceipt;  
 import org.jsmpp.bean.MessageType;  
 import org.jsmpp.extra.ProcessRequestException;  
 import org.jsmpp.session.DataSmResult;  
 import org.jsmpp.session.MessageReceiverListener;  
 import org.jsmpp.session.Session;  
 import org.jsmpp.util.InvalidDeliveryReceiptException;  
 public class MessageReceiverListenerImpl implements MessageReceiverListener {  
   public void onAcceptDeliverSm(DeliverSm deliverSm)  
       throws ProcessRequestException {  
     if (MessageType.SMSC_DEL_RECEIPT.containedIn(deliverSm.getEsmClass())) {  
       // this message is delivery receipt  
       try {  
         DeliveryReceipt delReceipt = deliverSm  
             .getShortMessageAsDeliveryReceipt();  
         // lets cover the id to hex string format  
         long id = Long.parseLong(delReceipt.getId()) & 0xffffffff;  
         String messageId = Long.toString(id, 16).toUpperCase();  
         /*  
          * you can update the status of your submitted message on the  
          * database based on messageId  
          */  
         System.out.println("Receiving delivery receipt for message '"  
             + messageId + " ' from " + deliverSm.getSourceAddr()  
             + " to " + deliverSm.getDestAddress() + " : "  
             + delReceipt);  
       } catch (InvalidDeliveryReceiptException e) {  
         System.err.println("Failed getting delivery receipt");  
         e.printStackTrace();  
       }  
     } else {  
       // this message is regular short message  
       /*  
        * you can save the incoming message to database.  
        */  
       System.out.println("Receiving message : "  
           + new String(deliverSm.getShortMessage()));  
     }  
   }  
   public void onAcceptAlertNotification(AlertNotification alertNotification) {  
   }  
   public DataSmResult onAcceptDataSm(DataSm dataSm, Session source)  
       throws ProcessRequestException {  
     return null;  
   }  
 }  

This class implements JSMPP Message Receiver Listener and in this snippet, we will focus on onDeliverSM() only, it will execute the logic inside it when delivered message is accepted. See, here we only create a delivery receipt by getting the message ID and print it.

Now, the last one is the receiver class

 import java.io.IOException;  
 import java.util.concurrent.TimeoutException;  
 import org.apache.log4j.BasicConfigurator;  
 import org.jsmpp.PDUStringException;  
 import org.jsmpp.SMPPConstant;  
 import org.jsmpp.bean.CancelSm;  
 import org.jsmpp.bean.DataSm;  
 import org.jsmpp.bean.QuerySm;  
 import org.jsmpp.bean.ReplaceSm;  
 import org.jsmpp.bean.SubmitMulti;  
 import org.jsmpp.bean.SubmitMultiResult;  
 import org.jsmpp.bean.SubmitSm;  
 import org.jsmpp.extra.ProcessRequestException;  
 import org.jsmpp.session.BindRequest;  
 import org.jsmpp.session.DataSmResult;  
 import org.jsmpp.session.QuerySmResult;  
 import org.jsmpp.session.SMPPServerSession;  
 import org.jsmpp.session.SMPPServerSessionListener;  
 import org.jsmpp.session.ServerMessageReceiverListener;  
 import org.jsmpp.session.Session;  
 import org.jsmpp.util.MessageIDGenerator;  
 import org.jsmpp.util.MessageId;  
 import org.jsmpp.util.RandomMessageIDGenerator;  
 public class ReceiveSubmittedMessageExample {  
   public static void main(String[] args) {  
     BasicConfigurator.configure();  
     try {  
       // prepare generator of Message ID  
       final MessageIDGenerator messageIdGenerator = new RandomMessageIDGenerator();  
       // prepare the message receiver  
       ServerMessageReceiverListener messageReceiverListener = new ServerMessageReceiverListener() {  
         public MessageId onAcceptSubmitSm(SubmitSm submitSm,  
             SMPPServerSession source)  
             throws ProcessRequestException {  
           System.out.println("Receiving message : " + new String(submitSm.getShortMessage()));  
           // need message_id to response submit_sm  
           return messageIdGenerator.newMessageId();  
         }  
         public QuerySmResult onAcceptQuerySm(QuerySm querySm,  
             SMPPServerSession source)  
             throws ProcessRequestException {  
           return null;  
         }  
         public SubmitMultiResult onAcceptSubmitMulti(  
             SubmitMulti submitMulti, SMPPServerSession source)  
             throws ProcessRequestException {  
           return null;  
         }  
         public DataSmResult onAcceptDataSm(DataSm dataSm, Session source)  
             throws ProcessRequestException {  
           return null;  
         }  
         public void onAcceptCancelSm(CancelSm cancelSm,  
             SMPPServerSession source)  
             throws ProcessRequestException {  
         }  
         public void onAcceptReplaceSm(ReplaceSm replaceSm,  
             SMPPServerSession source)  
             throws ProcessRequestException {  
         }  
       };  
       System.out.println("Listening ...");  
       SMPPServerSessionListener sessionListener = new SMPPServerSessionListener(8056);  
       // set all default ServerMessageReceiverListener for all accepted SMPPServerSessionListener  
       sessionListener.setMessageReceiverListener(messageReceiverListener);  
       // accepting connection, session still in OPEN state  
       SMPPServerSession session = sessionListener.accept();  
       // or we can set for each accepted session session.setMessageReceiverListener(messageReceiverListener)  
       System.out.println("Accept connection");  
       try {  
         BindRequest request = session.waitForBind(5000);  
         System.out.println("Receive bind request");  
         if (request.getSystemId().equals("test") &&  
             request.getPassword().equals("test")) {  
           // accepting request and send bind response immediately  
           System.out.println("Accepting bind request");  
           request.accept("sys");  
           try { Thread.sleep(20000); } catch (InterruptedException e) {}  
         } else {  
           System.out.println("Rejecting bind request");  
           request.reject(SMPPConstant.STAT_ESME_RINVPASWD);  
         }  
       } catch (TimeoutException e) {  
         System.out.println("No binding request made after 5000 millisecond");  
         e.printStackTrace();  
       }  
       System.out.println("Closing session");  
       session.unbindAndClose();  
       System.out.println("Closing session listener");  
       sessionListener.close();  
     } catch (PDUStringException e) {  
       e.printStackTrace();  
     } catch (IOException e) {  
       e.printStackTrace();  
     }  
   }  

Done, try to run the receiver first and then the sender, the sender will send a message and print the delivery receipt of it while the receiver will receive the message and print its contents.

For further references, please see https://code.google.com/p/jsmpp/


Comments

  1. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  2. Hi, SMPP is an industry protocol for sending text messages which are used to create and bind several SMPP connectivities.Great benefits in Bulk SMS Business.SMPP Server Provider

    ReplyDelete

Post a Comment

Popular Posts