如何使用deliver方法使用opensmpp发送短消息?

时间:2017-05-24 09:00:44

标签: java smpp opensmpp

我使用opensmpp并尝试提交方法(Submit_SM),它工作正常。但后来我发现我必须使用传递方法(Deliver_SM)才能使用ESME的监听端并从SMSC接收消息。 这是我的代码绑定工作正常,但来自deliver方法的响应始终为null。 如果有人可以帮助我,或者至少给我一个交付方法的例子 PS:我使用同步模式。

import org.smpp.pdu.BindRequest
import org.smpp.pdu.BindResponse
import org.smpp.pdu.BindTransciever
import org.smpp.pdu.BindTransmitter
import org.smpp.pdu.BindReceiver
import org.smpp.TCPIPConnection
import org.smpp.Session
import org.smpp.test.SMPPTest.SMPPTestPDUEventListener
import org.smpp.pdu.AddressRange
import org.smpp.Data
import org.smpp.pdu.UnbindResp
import org.smpp.pdu.DeliverSM
import org.smpp.pdu.DataSM
import org.smpp.pdu.DeliverSMResp
import org.smpp.pdu.DataSMResp
import org.smpp.pdu.PDU
import org.smpp.pdu.Request
import org.smpp.pdu.Response
import org.smpp.ServerPDUEvent
import org.smpp.pdu.Address
import org.smpp.pdu.EnquireLink
import org.smpp.pdu.EnquireLinkResp

Session session = null;
def bound = false
boolean asynchronous = false;
String bindOption = "tr";
String systemType = "";
String serviceType = "";
Address sourceAddress = new Address()
Address destAddress = new Address()
byte replaceIfPresentFlag = 0;
String shortMessage = ""; // the message to send
String scheduleDeliveryTime = "";
String validityPeriod = "" // default estimated validity default period
byte esmClass = 0;
byte protocolId = 0;
byte priorityFlag = 0;
byte registeredDelivery = 0;
byte dataCoding = 0;
byte smDefaultMsgId = 0;
String messageId = "";
long receiveTimeout = Data.RECEIVE_BLOCKING;
AddressRange addressRange = new AddressRange();
SMPPTestPDUEventListener pduListener = null;

    println "begin binding operation..."
    try {

        // test if communication is already established
        if (bound) {
            System.out.println("Already bound, unbind first.");
            return;
        }

        BindRequest request = null;
        BindResponse response = null;
        // type of the session
        String syncMode = (asynchronous ? "a" : "s");

        // input values


        if (bindOption.compareToIgnoreCase("t") == 0) {
            request = new BindTransmitter();
        } else if (bindOption.compareToIgnoreCase("r") == 0) {
            request = new BindReceiver();
        } else if (bindOption.compareToIgnoreCase("tr") == 0) {
            request = new BindTransciever();
        }

        ipAddress = "xxx.xxx.xx.xx";
        port = yyyy;

        println "syncMode == "+syncMode
        TCPIPConnection connection = new TCPIPConnection(ipAddress, port);
        connection.setReceiveTimeout(20 * 1000);
        session = new Session(connection);

        systemId = "nnnnnn"
        password = "nnnnnn"

        // set values
        request.setSystemId(systemId);
        request.setPassword(password);
        request.setSystemType(systemType);
        request.setInterfaceVersion((byte) 0x34);
        request.setAddressRange(addressRange);

        // send the request
        System.out.println("Bind request " + request.debugString());
        if (asynchronous) {
            pduListener = new SMPPTestPDUEventListener(session);
            response = session.bind(request, pduListener);
        } else {
            response = session.bind(request);
        }
        System.out.println("Bind response " + response.debugString());
        println "response.getCommandStatus() === "+response.getCommandStatus()
        if (response.getCommandStatus() == Data.ESME_ROK) {
            bound = true;
            println "SMSC is bound succesfuuly with the ESME"
        }

    } catch (Exception e) {
        //event.write(e, "");
        //debug.write("Bind operation failed. " + e);
        System.out.println("Bind operation failed. " + e);
    } finally {
        //session.unbind()
        //connection.close()
    }

    println "enquire data ... "
    try {
        final EnquireLink request = new EnquireLink();
        System.out.println("Enquire Link request " + request.debugString());
        final EnquireLinkResp response = session.enquireLink(request);
        System.out.println("Enquire Link response " + response.debugString());
    } catch (Throwable e) {
        e.printStackTrace();
    }


try {
    DeliverSM request = new DeliverSM();
    DeliverSMResp response;

            // input values
            serviceType = ""
            sourceAddress.setNpi((byte)1)
    sourceAddress.setTon((byte)1)
    sourceAddress.setAddress("4123601", Data.SM_ADDR_LEN)
            destAddress.setNpi((byte)1)
    destAddress.setTon((byte)1)
    destAddress.setAddress("0022233411686", Data.SM_ADDR_LEN)
            esmClass = 0
            registeredDelivery = 0;
            dataCoding = 0;

           // set values
    request.setServiceType(serviceType);
            request.setSequenceNumber(1)
    request.setSourceAddr(sourceAddress);
    request.setDestAddr(destAddress);
    request.setEsmClass(esmClass);
    request.setRegisteredDelivery(registeredDelivery);
    request.setDataCoding(dataCoding);
            request.setProtocolId(protocolId)
            request.setPriorityFlag(priorityFlag);
            request.setShortMessage("Hello world",Data.ENC_ASCII);

    // send the request
    System.out.println("Data request " + request.debugString());
    if (asynchronous) {
        session.data(request);
    } else {
        response = session.deliver(request); // here the problem , the response is always null
        System.out.println("Data response " + response.debugString());
        messageId = response.getMessageId();
    }

} catch (Exception e) {
        System.out.println("Data operation failed. " + e);
    }
finally {
       session.unbind()
}

1 个答案:

答案 0 :(得分:0)

尝试使用'PDU response = session.receive(0)'而不是'response = session.deliver(request)'。参数0表示不等待答案,因此它可以返回null。但是,当交付到达时你会得到它。之后,您将必须处理响应:

        int commandId = pdu.getCommandId();

        switch(commandId) {
            case Data.DELIVER_SM:
                DeliverSM pduDeliver = (DeliverSM)pdu;
                procesarDeliver(pduDeliver);
                break;

            case Data.ENQUIRE_LINK:
                //Do something
                break;

            case Data.UNBIND:
                //Do something
                break;

            default:
                //Do something
                break;
        }

对不起我的英语......;)