java—在jsmpp中创建单个连接而不是多个连接

a5g8bdjr  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(398)

我正在使用 JSMPP 我已经和 smsc 我在试着发信息,试着收到送货收据
我正面临一个问题,我只想创建一个连接。如果你能帮我,那就太好了。我的代码是:

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"; 
}

}

请帮我弄清楚我的问题

mmvthczy

mmvthczy1#

将smppsession对象作为示例变量移动,以便会话对象保持不变。

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"; 
}

}
e5nszbig

e5nszbig2#

您必须创建一个全局静态smppsession变量,如下所示。

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

}

相关问题