-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add: simple SMS send and receive Sample Code
- Loading branch information
Showing
1 changed file
with
153 additions
and
0 deletions.
There are no files selected for viewing
153 changes: 153 additions & 0 deletions
153
jsmpp-examples/src/main/java/org/jsmpp/examples/SimpleSubmitSimpleReceiveExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
package org.jsmpp.examples; | ||
|
||
import java.io.IOException; | ||
import java.util.Date; | ||
|
||
import org.apache.log4j.BasicConfigurator; | ||
import org.jsmpp.InvalidResponseException; | ||
import org.jsmpp.PDUException; | ||
import org.jsmpp.bean.AlertNotification; | ||
import org.jsmpp.bean.Alphabet; | ||
import org.jsmpp.bean.BindType; | ||
import org.jsmpp.bean.DataSm; | ||
import org.jsmpp.bean.DeliverSm; | ||
import org.jsmpp.bean.DeliveryReceipt; | ||
import org.jsmpp.bean.ESMClass; | ||
import org.jsmpp.bean.GeneralDataCoding; | ||
import org.jsmpp.bean.MessageClass; | ||
import org.jsmpp.bean.MessageType; | ||
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.ProcessRequestException; | ||
import org.jsmpp.extra.ResponseTimeoutException; | ||
import org.jsmpp.session.BindParameter; | ||
import org.jsmpp.session.DataSmResult; | ||
import org.jsmpp.session.MessageReceiverListener; | ||
import org.jsmpp.session.SMPPSession; | ||
import org.jsmpp.session.Session; | ||
import org.jsmpp.util.AbsoluteTimeFormatter; | ||
import org.jsmpp.util.InvalidDeliveryReceiptException; | ||
import org.jsmpp.util.TimeFormatter; | ||
|
||
/** | ||
* @author terukizm | ||
* | ||
*/ | ||
public class SimpleSubmitSimpleReceiveExample { | ||
private static TimeFormatter timeFormatter = new AbsoluteTimeFormatter();; | ||
|
||
public static void main(String[] args) { | ||
String server = "localhost"; | ||
int port = 8056; | ||
String message = "jSMPP simplify SMPP on Java platform"; | ||
|
||
// bind(connect) | ||
SMPPSession session = new SMPPSession(); | ||
try { | ||
session.connectAndBind(server, port, 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(); | ||
} | ||
|
||
// send Message | ||
try { | ||
// set RegisteredDelivery | ||
final RegisteredDelivery registeredDelivery = new RegisteredDelivery(); | ||
registeredDelivery.setSMSCDeliveryReceipt(SMSCDeliveryReceipt.SUCCESS_FAILURE); | ||
|
||
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, | ||
registeredDelivery, (byte)0, new GeneralDataCoding(Alphabet.ALPHA_DEFAULT, MessageClass.CLASS1, | ||
false), (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(); | ||
} | ||
|
||
// receive Message | ||
BasicConfigurator.configure(); | ||
|
||
// Set listener to receive deliver_sm | ||
session.setMessageReceiverListener(new MessageReceiverListener() { | ||
|
||
public void onAcceptDeliverSm(DeliverSm deliverSm) throws ProcessRequestException { | ||
if (MessageType.SMSC_DEL_RECEIPT.containedIn(deliverSm.getEsmClass())) { | ||
// delivery receipt | ||
try { | ||
DeliveryReceipt delReceipt = deliverSm.getShortMessageAsDeliveryReceipt(); | ||
long id = Long.parseLong(delReceipt.getId()) & 0xffffffff; | ||
String messageId = Long.toString(id, 16).toUpperCase(); | ||
System.out.println("received '" + messageId + "' : " + delReceipt); | ||
} catch (InvalidDeliveryReceiptException e) { | ||
System.err.println("receive faild"); | ||
e.printStackTrace(); | ||
} | ||
} else { | ||
// regular short message | ||
System.out.println("Receiving message : " + new String(deliverSm.getShortMessage())); | ||
} | ||
} | ||
|
||
public void onAcceptAlertNotification(AlertNotification alertNotification) { | ||
System.out.println("onAcceptAlertNotification"); | ||
} | ||
|
||
public DataSmResult onAcceptDataSm(DataSm dataSm, Session source) throws ProcessRequestException { | ||
System.out.println("onAcceptDataSm"); | ||
return null; | ||
} | ||
}); | ||
|
||
// wait 3 second | ||
try { | ||
Thread.sleep(3000); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
// unbind(disconnect) | ||
session.unbindAndClose(); | ||
|
||
System.out.println("finish!"); | ||
} | ||
|
||
} |
77fd644
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this example is not working because GeneralDataCoding class inside API thre 4 parameter in contructor but in example , parameter been passed only three so how can i solve or modified it?
77fd644
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, plz
77fd644
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@javalok
jsmpp/jsmpp/src/main/java/org/jsmpp/bean/GeneralDataCoding.java
Line 57 in 7791dab
77fd644
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ADTC i tried this sample code
public class SimpleSubmitExample {
}
but ,
here is problem in :
new GeneralDataCoding(Alphabet.ALPHA_DEFAULT, MessageClass.CLASS1, false)
because , i looked for GeneralDataCoding() class there is not three parameterized constructor so can you give me right code for me to test?
thanks
77fd644
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right code: https://github.com/uudashr/jsmpp
As long as you use this, you should not face any issue.
Yes, that's not a mistake. It's a link to this project's home page. It's where the right code is. You can click on Download ZIP to simply get the plain source code, or just clone the git repo if you have a git client installed.
77fd644
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
77fd644
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.