Skip to content

Commit 5fc2bc6

Browse files
committed
Update examples to use slf4j-api.
1 parent 868ef9a commit 5fc2bc6

17 files changed

+480
-469
lines changed

jsmpp-examples/src/main/java/org/jsmpp/examples/AcceptingConnectionAndBindExample.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,61 +17,60 @@
1717
import java.io.IOException;
1818
import java.util.concurrent.TimeoutException;
1919

20-
import org.apache.log4j.BasicConfigurator;
2120
import org.jsmpp.PDUStringException;
2221
import org.jsmpp.SMPPConstant;
2322
import org.jsmpp.session.BindRequest;
2423
import org.jsmpp.session.SMPPServerSession;
2524
import org.jsmpp.session.SMPPServerSessionListener;
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
2627

2728
/**
2829
* @author uudashr
2930
*
3031
*/
3132
public class AcceptingConnectionAndBindExample {
33+
private static final Logger LOGGER = LoggerFactory.getLogger(AcceptingConnectionAndBindExample.class);
3234

3335
public static void main(String[] args) {
34-
BasicConfigurator.configure();
3536
try {
36-
System.out.println("Listening ...");
37+
LOGGER.info("Listening ...");
3738
SMPPServerSessionListener sessionListener = new SMPPServerSessionListener(8056);
3839

3940
// accepting connection, session still in OPEN state
4041
SMPPServerSession session = sessionListener.accept();
41-
System.out.println("Accept connection");
42+
LOGGER.info("Accept connection");
4243

4344
try {
4445
BindRequest request = session.waitForBind(5000);
45-
System.out.println("Receive bind request");
46+
LOGGER.info("Receive bind request");
4647

4748
if ("test".equals(request.getSystemId()) &&
4849
"test".equals(request.getPassword())) {
49-
50+
5051
// accepting request and send bind response immediately
51-
System.out.println("Accepting bind request, interface version is " + request.getInterfaceVersion());
52+
LOGGER.info("Accepting bind request, interface version is " + request.getInterfaceVersion());
5253
request.accept("sys");
5354

5455
try { Thread.sleep(20000); } catch (InterruptedException e) {}
5556
} else {
56-
System.out.println("Rejecting bind request");
57+
LOGGER.info("Rejecting bind request");
5758
request.reject(SMPPConstant.STAT_ESME_RINVPASWD);
5859
}
5960
} catch (TimeoutException e) {
60-
System.out.println("No binding request made after 5000 millisecond");
61-
e.printStackTrace();
61+
LOGGER.error("No binding request made after 5000 millisecond", e);
6262
}
6363

64-
6564
if (session.getSessionState().isBound()) {
66-
System.out.println("Closing session");
65+
LOGGER.info("Closing session");
6766
session.unbindAndClose();
6867
}
69-
System.out.println("Closing session listener");
68+
LOGGER.info("Closing session listener");
7069
sessionListener.close();
7170
} catch (PDUStringException e) {
72-
e.printStackTrace();
71+
LOGGER.error("PDUStringException", e);
7372
} catch (IOException e) {
74-
e.printStackTrace();
73+
LOGGER.error("IOException", e);
7574
}
7675

7776
}

jsmpp-examples/src/main/java/org/jsmpp/examples/AsyncSubmitReceiveDeliverSmExample.java

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.concurrent.Executors;
2121
import java.util.concurrent.atomic.AtomicInteger;
2222

23-
import org.apache.log4j.BasicConfigurator;
2423
import org.jsmpp.InvalidResponseException;
2524
import org.jsmpp.PDUException;
2625
import org.jsmpp.bean.AlertNotification;
@@ -46,23 +45,25 @@
4645
import org.jsmpp.util.AbsoluteTimeFormatter;
4746
import org.jsmpp.util.InvalidDeliveryReceiptException;
4847
import org.jsmpp.util.TimeFormatter;
48+
import org.slf4j.Logger;
49+
import org.slf4j.LoggerFactory;
4950

5051
/**
5152
* @author uudashr
5253
*
5354
*/
5455
public class AsyncSubmitReceiveDeliverSmExample {
55-
private static TimeFormatter timeFormatter = new AbsoluteTimeFormatter();
56+
private static final Logger LOGGER = LoggerFactory.getLogger(AsyncSubmitReceiveDeliverSmExample.class);
57+
private static final TimeFormatter TIME_FORMATTER = new AbsoluteTimeFormatter();
58+
5659
public static void main(String[] args) {
5760
final AtomicInteger counter = new AtomicInteger();
5861

59-
BasicConfigurator.configure();
6062
final SMPPSession session = new SMPPSession();
6163
try {
6264
session.connectAndBind("localhost", 8056, new BindParameter(BindType.BIND_TRX, "test", "test", "cp", TypeOfNumber.UNKNOWN, NumberingPlanIndicator.UNKNOWN, null));
6365
} catch (IOException e) {
64-
System.err.println("Failed connect and bind to host");
65-
e.printStackTrace();
66+
LOGGER.error("Failed connect and bind to host", e);
6667
}
6768

6869
// Set listener to receive deliver_sm
@@ -76,14 +77,13 @@ public void onAcceptDeliverSm(DeliverSm deliverSm)
7677
DeliveryReceipt delReceipt = deliverSm.getShortMessageAsDeliveryReceipt();
7778
long id = Long.parseLong(delReceipt.getId()) & 0xffffffff;
7879
String messageId = Long.toString(id, 16).toUpperCase();
79-
System.out.println("Receiving delivery receipt for message '" + messageId + "' : " + delReceipt);
80+
LOGGER.info("Receiving delivery receipt for message '{}' : {}", messageId, delReceipt);
8081
} catch (InvalidDeliveryReceiptException e) {
81-
System.err.println("Failed getting delivery receipt");
82-
e.printStackTrace();
82+
LOGGER.error("Failed getting delivery receipt", e);
8383
}
8484
} else {
8585
// regular short message
86-
System.out.println("Receiving message : " + new String(deliverSm.getShortMessage()));
86+
LOGGER.info("Receiving message : {}", new String(deliverSm.getShortMessage()));
8787
}
8888
}
8989

@@ -110,29 +110,25 @@ public DataSmResult onAcceptDataSm(DataSm dataSm, Session source)
110110
execService.execute(new Runnable() {
111111
public void run() {
112112
try {
113-
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, DataCodings.ZERO, (byte)0, "jSMPP simplify SMPP on Java platform".getBytes());
114-
System.out.println("Message submitted, message_id is " + messageId);
113+
String messageId = session.submitShortMessage("CMT", TypeOfNumber.INTERNATIONAL, NumberingPlanIndicator.UNKNOWN, "1616", TypeOfNumber.INTERNATIONAL, NumberingPlanIndicator.UNKNOWN, "628176504657", new ESMClass(), (byte)0, (byte)1, TIME_FORMATTER
114+
.format(new Date()), null, registeredDelivery, (byte)0, DataCodings.ZERO, (byte)0, "jSMPP simplify SMPP on Java platform".getBytes());
115+
LOGGER.info("Message submitted, message_id is {}", messageId);
115116
} catch (PDUException e) {
116-
System.err.println("Invalid PDU parameter");
117-
e.printStackTrace();
117+
LOGGER.error("Invalid PDU parameter", e);
118118
counter.incrementAndGet();
119119
} catch (ResponseTimeoutException e) {
120-
System.err.println("Response timeout");
121-
e.printStackTrace();
120+
LOGGER.error("Response timeout", e);
122121
counter.incrementAndGet();
123122
} catch (InvalidResponseException e) {
124123
// Invalid response
125-
System.err.println("Receive invalid respose");
126-
e.printStackTrace();
124+
LOGGER.error("Receive invalid respose", e);
127125
counter.incrementAndGet();
128126
} catch (NegativeResponseException e) {
129127
// Receiving negative response (non-zero command_status)
130-
System.err.println("Receive negative response");
131-
e.printStackTrace();
128+
LOGGER.error("Receive negative response", e);
132129
counter.incrementAndGet();
133130
} catch (IOException e) {
134-
System.err.println("IO error occur");
135-
e.printStackTrace();
131+
LOGGER.error("I/O error occured", e);
136132
counter.incrementAndGet();
137133
}
138134
}
@@ -142,7 +138,7 @@ public void run() {
142138
while (counter.get() != maxMessage) {
143139
try { Thread.sleep(1000); }
144140
catch (InterruptedException e) {
145-
System.err.println("Interrupted");
141+
LOGGER.error("Interrupted");
146142
}
147143
}
148144
session.unbindAndClose();

jsmpp-examples/src/main/java/org/jsmpp/examples/MessageReceiverListenerImpl.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515
package org.jsmpp.examples;
1616

17+
import org.jsmpp.SMPPConstant;
1718
import org.jsmpp.bean.AlertNotification;
1819
import org.jsmpp.bean.DataSm;
1920
import org.jsmpp.bean.DeliverSm;
@@ -24,12 +25,17 @@
2425
import org.jsmpp.session.MessageReceiverListener;
2526
import org.jsmpp.session.Session;
2627
import org.jsmpp.util.InvalidDeliveryReceiptException;
28+
import org.slf4j.Logger;
29+
import org.slf4j.LoggerFactory;
2730

2831
/**
2932
* @author uudashr
3033
*
3134
*/
3235
public class MessageReceiverListenerImpl implements MessageReceiverListener {
36+
private static final Logger LOGGER = LoggerFactory.getLogger(MessageReceiverListenerImpl.class);
37+
private static final String DATASM_NOT_IMPLEMENTED = "data_sm not implemented";
38+
3339
public void onAcceptDeliverSm(DeliverSm deliverSm)
3440
throws ProcessRequestException {
3541

@@ -46,11 +52,11 @@ public void onAcceptDeliverSm(DeliverSm deliverSm)
4652
* you can update the status of your submitted message on the
4753
* database based on messageId
4854
*/
49-
50-
System.out.println("Receiving delivery receipt for message '" + messageId + " ' from " + deliverSm.getSourceAddr() + " to " + deliverSm.getDestAddress() + " : " + delReceipt);
55+
56+
LOGGER.info("Receiving delivery receipt for message '{}' from {} to {}: {}",
57+
messageId, deliverSm.getSourceAddr(), deliverSm.getDestAddress(), delReceipt);
5158
} catch (InvalidDeliveryReceiptException e) {
52-
System.err.println("Failed getting delivery receipt");
53-
e.printStackTrace();
59+
LOGGER.error("Failed getting delivery receipt", e);
5460
}
5561
} else {
5662
// this message is regular short message
@@ -59,15 +65,17 @@ public void onAcceptDeliverSm(DeliverSm deliverSm)
5965
* you can save the incoming message to database.
6066
*/
6167

62-
System.out.println("Receiving message : " + new String(deliverSm.getShortMessage()));
68+
LOGGER.info("Receiving message : {}", new String(deliverSm.getShortMessage()));
6369
}
6470
}
6571

6672
public void onAcceptAlertNotification(AlertNotification alertNotification) {
73+
LOGGER.info("AlertNotification not implemented");
6774
}
6875

6976
public DataSmResult onAcceptDataSm(DataSm dataSm, Session source)
7077
throws ProcessRequestException {
71-
return null;
78+
LOGGER.info("DataSm not implemented");
79+
throw new ProcessRequestException(DATASM_NOT_IMPLEMENTED, SMPPConstant.STAT_ESME_RINVCMDID);
7280
}
7381
}

jsmpp-examples/src/main/java/org/jsmpp/examples/OpenAndBindExample.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,34 @@
1616

1717
import java.io.IOException;
1818

19-
import org.apache.log4j.BasicConfigurator;
2019
import org.jsmpp.bean.BindType;
2120
import org.jsmpp.bean.NumberingPlanIndicator;
2221
import org.jsmpp.bean.TypeOfNumber;
2322
import org.jsmpp.session.BindParameter;
2423
import org.jsmpp.session.SMPPSession;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
2526

2627
/**
2728
* @author uudashr
2829
*
2930
*/
3031
public class OpenAndBindExample {
32+
private static final Logger LOGGER = LoggerFactory.getLogger(OpenAndBindExample.class);
3133

3234
public static void main(String[] args) {
33-
BasicConfigurator.configure();
3435
String host = "localhost";
3536
int port = 8056;
3637
SMPPSession session = new SMPPSession();
3738
try {
38-
System.out.println("Connect and bind to " + host + " port " + port);
39+
LOGGER.info("Connect and bind to {} port {}", host, port);
3940
session.connectAndBind(host, port, new BindParameter(BindType.BIND_TRX, "test", "test", "cp", TypeOfNumber.UNKNOWN, NumberingPlanIndicator.UNKNOWN, null));
4041
} catch (IOException e) {
4142
// Failed connect and bind to SMSC
42-
System.err.println("Failed connect and bind to host");
43-
e.printStackTrace();
43+
LOGGER.error("Failed connect and bind to host", e);
4444
}
4545
try { Thread.sleep(10000); } catch (InterruptedException e) {}
4646
session.unbindAndClose();
4747
}
48-
49-
48+
5049
}

jsmpp-examples/src/main/java/org/jsmpp/examples/ReceiveSubmittedMessageExample.java

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import java.io.IOException;
1818
import java.util.concurrent.TimeoutException;
1919

20-
import org.apache.log4j.BasicConfigurator;
2120
import org.jsmpp.PDUStringException;
2221
import org.jsmpp.SMPPConstant;
2322
import org.jsmpp.bean.CancelSm;
@@ -38,27 +37,28 @@
3837
import org.jsmpp.util.MessageIDGenerator;
3938
import org.jsmpp.util.MessageId;
4039
import org.jsmpp.util.RandomMessageIDGenerator;
40+
import org.slf4j.Logger;
41+
import org.slf4j.LoggerFactory;
4142

4243
/**
4344
* @author uudashr
4445
*
4546
*/
4647
public class ReceiveSubmittedMessageExample {
48+
49+
private static final Logger LOGGER = LoggerFactory.getLogger(ReceiveSubmittedMessageExample.class);
4750

4851
public static void main(String[] args) {
49-
BasicConfigurator.configure();
5052
try {
51-
52-
53+
5354
// prepare generator of Message ID
5455
final MessageIDGenerator messageIdGenerator = new RandomMessageIDGenerator();
5556

5657
// prepare the message receiver
5758
ServerMessageReceiverListener messageReceiverListener = new ServerMessageReceiverListener() {
58-
public MessageId onAcceptSubmitSm(SubmitSm submitSm,
59-
SMPPServerSession source)
59+
public MessageId onAcceptSubmitSm(SubmitSm submitSm, SMPPServerSession source)
6060
throws ProcessRequestException {
61-
System.out.println("Receiving message : " + new String(submitSm.getShortMessage()));
61+
LOGGER.info("Receiving message : {}", new String(submitSm.getShortMessage()));
6262
// need message_id to response submit_sm
6363
return messageIdGenerator.newMessageId();
6464
}
@@ -91,46 +91,44 @@ public void onAcceptReplaceSm(ReplaceSm replaceSm,
9191
}
9292
};
9393

94-
System.out.println("Listening ...");
94+
LOGGER.info("Listening ...");
9595
SMPPServerSessionListener sessionListener = new SMPPServerSessionListener(8056);
9696
// set all default ServerMessageReceiverListener for all accepted SMPPServerSessionListener
9797
sessionListener.setMessageReceiverListener(messageReceiverListener);
9898

9999
// accepting connection, session still in OPEN state
100100
SMPPServerSession session = sessionListener.accept();
101101
// or we can set for each accepted session session.setMessageReceiverListener(messageReceiverListener)
102-
System.out.println("Accept connection");
102+
LOGGER.info("Accept connection");
103103

104104
try {
105105
BindRequest request = session.waitForBind(5000);
106-
System.out.println("Receive bind request");
106+
LOGGER.info("Receive bind request for system id {} and password {}", request.getSystemId(), request.getSystemId(), request.getPassword());
107107

108108
if ("test".equals(request.getSystemId()) &&
109109
"test".equals(request.getPassword())) {
110110

111111
// accepting request and send bind response immediately
112-
System.out.println("Accepting bind request");
112+
LOGGER.info("Accepting bind request");
113113
request.accept("sys");
114-
115-
114+
116115
try { Thread.sleep(20000); } catch (InterruptedException e) {}
117116
} else {
118-
System.out.println("Rejecting bind request");
117+
LOGGER.info("Rejecting bind request");
119118
request.reject(SMPPConstant.STAT_ESME_RINVPASWD);
120119
}
121120
} catch (TimeoutException e) {
122-
System.out.println("No binding request made after 5000 millisecond");
123-
e.printStackTrace();
121+
LOGGER.error("No binding request made after 5000 millisecond", e);
124122
}
125-
126-
System.out.println("Closing session");
123+
124+
LOGGER.info("Closing session");
127125
session.unbindAndClose();
128-
System.out.println("Closing session listener");
126+
LOGGER.info("Closing session listener");
129127
sessionListener.close();
130128
} catch (PDUStringException e) {
131-
e.printStackTrace();
129+
LOGGER.error("PDUString exception", e);
132130
} catch (IOException e) {
133-
e.printStackTrace();
131+
LOGGER.error("I/O exception", e);
134132
}
135133
}
136134
}

0 commit comments

Comments
 (0)