本文介绍了在jsmpp中创建单个连接不是多个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用JSMPP
,我已与smsc
连接,并且正在尝试发送消息并尝试接收收货收据
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";
}
}
请帮助我找出问题所在
推荐答案
您必须创建一个全局静态SMPPSession变量,如下所示.
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();
}
}
}
这篇关于在jsmpp中创建单个连接不是多个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!