Creating single conection in jsmpp not multiple

2019-09-08 08:15发布

I am using JSMPP I have connected with the smsc and I am trying to send messages and trying to receive the delivery receipts

I am facing a problem I just want to create a single connection. If you can help me out here that would be really great. My code is:

package com.jsmpp.dialerintegration;
public class SenderToMsgBroker{ 

public  String  sendingSms(String [] mobileNo) {  
    String [] receiverMobileNo = mobileNo;
    String blankMSG="";

    SMPPSession session = new SMPPSession();

    try{

         session.connectAndBind("ip", port,
                new BindParameter(BindType.BIND_TRX, "username", "password",
                        "cp", TypeOfNumber.UNKNOWN,
                        NumberingPlanIndicator.UNKNOWN, null),1000*60*30,session);
        System.out.println("Connection Established And SessionId :"+session.getSessionId());
        System.out.println("SessionState"+session.getSessionState());

        }
      catch (IOException e) {
        System.err.println("Failed connect and bind to host");
        e.printStackTrace();
    }

    try {


        for(int i=0;i<receiverMobileNo.length;i++)
        {   


        session.setMessageReceiverListener(new MessageReceiverListenerImpl());  

        System.out.println("Trying to send the message->>>");
        String messageId = session
                .submitShortMessage(
                        null,
                        TypeOfNumber.UNKNOWN,
                        NumberingPlanIndicator.UNKNOWN,
                        "51633",
                        TypeOfNumber.UNKNOWN,
                        NumberingPlanIndicator.UNKNOWN,
                        receiverMobileNo[i],
                        new ESMClass(),
                        (byte) 0,
                        (byte) 1,
                        null,
                        null,
                        new RegisteredDelivery(
                                SMSCDeliveryReceipt.SUCCESS_FAILURE),
                        (byte) 0,
                        new GeneralDataCoding((byte) 0xc0),
                        (byte) 0,blankMSG.getBytes());
        System.out.println("Message submitted, message_id is " + messageId);
        System.out.println("Message Receiver Mobile No is :"
                + receiverMobileNo[i]);


        }

    } catch (PDUException e) {
        // Invalid PDU parameter
        System.err.println("Invalid PDU parameter");

        e.printStackTrace();

        return "PDUException";
    } catch (ResponseTimeoutException e) {
        // Response timeout
        System.err.println("Response timeout");
        e.printStackTrace();
        return "ResponseTimeoutException";
    } catch (InvalidResponseException e) {
        // Invalid response
        System.err.println("Receive invalid respose");
        e.printStackTrace();
        return "InvalidResponseException";
    } catch (NegativeResponseException e) {
        // Receiving negative response (non-zero command_status)
        System.err.println("Receive negative response");
        e.printStackTrace();
        return "NegativeResponseException";
    } catch (IOException e) {
        System.err.println("IO error occur");
        e.printStackTrace();
    }
    catch (Exception e) {
         System.err.println("Exeption Occur");
         e.printStackTrace();
         return "Eror"; 
    }


    return "Success"; 
}

}

Help me figure out my issue please

标签: java jsmpp
2条回答
相关推荐>>
2楼-- · 2019-09-08 08:43

you have to create a global static SMPPSession variable like below.

public class ConfigAndBind {

public static SMPPSession session= new SMPPSession();;
public  void bind() {
    //String server = Utilities.ipAddress;
   // int port = Utilities.port;
   // session =
    session.setMessageReceiverListener(new MessageReceiverListenerImpl());
    try {
        session.connectAndBind("ip",6543, new BindParameter(BindType.BIND_TRX, "systemid", "password", "cp", TypeOfNumber.UNKNOWN, NumberingPlanIndicator.UNKNOWN, null));
    } catch (IOException e) {
        System.err.println("Failed connect and bind to host");
        e.printStackTrace();
    }
    BasicConfigurator.configure();

}

public void unbind() {
     session.unbindAndClose();

     System.out.println("finish!");
}
public static SMPPSession getSession() {
    return session;
}
}



public class MessageReceiverListenerImpl implements MessageReceiverListener {

@Override
public DataSmResult onAcceptDataSm(DataSm arg0, Session arg1)
        throws ProcessRequestException {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onAcceptAlertNotification(AlertNotification arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onAcceptDeliverSm(DeliverSm deliverSm)
        throws ProcessRequestException {
    // TODO Auto-generated method stub
    if (MessageType.SMSC_DEL_RECEIPT.containedIn(deliverSm.getEsmClass())) {
        try {
            DeliveryReceipt delReceipt = deliverSm.getShortMessageAsDeliveryReceipt();      
            System.out.println("received '" + delReceipt.getId() + "' : " + delReceipt);
        } catch (InvalidDeliveryReceiptException e) {
            System.err.println("receive faild");
            e.printStackTrace();
        }
    } else {
            System.out.println("Receiving message : " + new String(deliverSm.getShortMessage())); 
    }

}
}


public class SendSms {

private SMPPSession session;
private TimeFormatter timeFormatter = new AbsoluteTimeFormatter();

public void sendSms(String message,String msisdn) {
    session = ConfigAndBind.getSession();
    long TEN_SECONDS=10000;//millisecs

    Calendar date = Calendar.getInstance();
    long t= date.getTimeInMillis();
    Date scheduleDeliveryTime=new Date(t + (   TEN_SECONDS));


    try {
        String messageId = session.submitShortMessage("CMT", TypeOfNumber.INTERNATIONAL, NumberingPlanIndicator.ISDN, "6161", 
                TypeOfNumber.INTERNATIONAL, NumberingPlanIndicator.ISDN, msisdn, new ESMClass(), (byte)0, (byte)1, 
                timeFormatter.format(scheduleDeliveryTime), null, new RegisteredDelivery(SMSCDeliveryReceipt.SUCCESS_FAILURE), 
                (byte)0, DataCodings.ZERO, (byte)0, message.getBytes());

        System.out.println("Message submitted, message_id is " + messageId);

    } 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();
    } 
}

}
查看更多
家丑人穷心不美
3楼-- · 2019-09-08 08:58

move SmppSession object as instance variable so that the session object persists.

package com.jsmpp.dialerintegration;
public class SenderToMsgBroker{ 

private SMPPSession session;

private SMPPSession getSmppSesssion()
{
if(null != session && 
    session.getSessionState().isBound())
{
    return session;
}

 session = new SMPPSession();
try{

     session.connectAndBind("ip", port,
            new BindParameter(BindType.BIND_TRX, "username", "password",
                    "cp", TypeOfNumber.UNKNOWN,
                    NumberingPlanIndicator.UNKNOWN, null),1000*60*30,session);
    System.out.println("Connection Established And SessionId                 :"+session.getSessionId());
    System.out.println("SessionState"+session.getSessionState());

    }
  catch (IOException e) {
    System.err.println("Failed connect and bind to host");
    e.printStackTrace();
}

}

public  String  sendingSms(String [] mobileNo) {  
String [] receiverMobileNo = mobileNo;
String blankMSG="";

SMPPSession session = getSmppSession();

try {


    for(int i=0;i<receiverMobileNo.length;i++)
    {   


    session.setMessageReceiverListener(new MessageReceiverListenerImpl());  

    System.out.println("Trying to send the message->>>");
    String messageId = session
            .submitShortMessage(
                    null,
                    TypeOfNumber.UNKNOWN,
                    NumberingPlanIndicator.UNKNOWN,
                    "51633",
                    TypeOfNumber.UNKNOWN,
                    NumberingPlanIndicator.UNKNOWN,
                    receiverMobileNo[i],
                    new ESMClass(),
                    (byte) 0,
                    (byte) 1,
                    null,
                    null,
                    new RegisteredDelivery(
                            SMSCDeliveryReceipt.SUCCESS_FAILURE),
                    (byte) 0,
                    new GeneralDataCoding((byte) 0xc0),
                    (byte) 0,blankMSG.getBytes());
    System.out.println("Message submitted, message_id is " + messageId);
    System.out.println("Message Receiver Mobile No is :"
            + receiverMobileNo[i]);


    }

} catch (PDUException e) {
    // Invalid PDU parameter
    System.err.println("Invalid PDU parameter");

    e.printStackTrace();

    return "PDUException";
} catch (ResponseTimeoutException e) {
    // Response timeout
    System.err.println("Response timeout");
    e.printStackTrace();
    return "ResponseTimeoutException";
} catch (InvalidResponseException e) {
    // Invalid response
    System.err.println("Receive invalid respose");
    e.printStackTrace();
    return "InvalidResponseException";
} catch (NegativeResponseException e) {
    // Receiving negative response (non-zero command_status)
    System.err.println("Receive negative response");
    e.printStackTrace();
    return "NegativeResponseException";
} catch (IOException e) {
    System.err.println("IO error occur");
    e.printStackTrace();
}
catch (Exception e) {
     System.err.println("Exeption Occur");
     e.printStackTrace();
     return "Eror"; 
}


return "Success"; 
}

}
查看更多
登录 后发表回答