opentelecoms-org / jsmpp

SMPP implemented in Java
Apache License 2.0
230 stars 160 forks source link

Recommendations for reconnect logic #17

Open bodunov opened 8 years ago

bodunov commented 8 years ago

Hi, I am curious how would you recommend to implement reconnect logic on top of jsmpp. If one would use Camel-Smpp then this is already implemented there. But I see one issue with jsmpp implementation maybe you can clarify. When jsmpp is used for the client side and the connection to the server gets broken then AbstractSession.close() will be called. First thing it does it calls ctx.close(). And then it calls connection.close(). Within ctx.close() it will invoke all registered listeners to inform about state change to CLOSE. Camel-Smpp provides such listener and when it is invoked it will try to re-establish connection. When the connection is reestablished and all listeners return back AbstractSession.close() method then connection.close() is invoked, which actually closes newly established connection. Is my understanding right? If yes, why is it done that way? Or what would be the right approach to implement reconnection (basically I am also asking if Camel-Smpp did it right). Thanks.

bodunov commented 8 years ago

Okey.. when Camel-Smpp does reconnection it creates a new session and thus a new connection. So, connection().close() in AbstractSession actually closes old connection, so no problems with this part. Still one thing which a bit bothers me is that reconnection happens as part of whole close procedure and before new session is successfully created Camel-Smpp blocks the closing of old one.. Maybe it would make sense to do reconnect in parallel thread? Hm...

dacristo commented 8 years ago

This solution seems to be working ok for me:

`public void connectAndBind() { session = new SMPPSession(); session.setMessageReceiverListener(new MessageReceiverListenerImpl()); try { BindType bindType = BindType.BIND_RX; TypeOfNumber addrTon = TypeOfNumber.INTERNATIONAL; // 0 NumberingPlanIndicator addrNpi = NumberingPlanIndicator.ISDN; // 1 String addressRange = null; String result = session.connectAndBind(ip_address, port, new BindParameter(bindType, system_id, password, "CMT", addrTon, addrNpi, addressRange)); } catch (IOException e) { e.printStackTrace(); session.unbindAndClose(); } session.addSessionStateListener(new SessionStateListener() {

        @Override
        public void onStateChange(SessionState newState, SessionState oldState,
                Object source) {                   
                    if (newState == SessionState.CLOSED) {
                        session.unbindAndClose();
                        connectAndBind();
                    }
                }
    });
}`
pmoerenhout commented 8 years ago

I recently used the Apache commons-pool2 library. I will add an example into the code.

ilkin commented 1 year ago

is it okay also to check Outbound and Unbound states for reconnection?

if (newState == SessionState.CLOSED || SessionState.OUTBOUND || SessionState.UNBOUND) {
    session.unbindAndClose();
    connectAndBind();
}