Sikander
Sikander

Reputation: 862

Binding issue with smpp server using jsmpp

I have a java application which uses jsmpp library to send SMSs to SMSC. Application connects successfully and sends SMSs. Connection issue occurs after a week or so up time, during this up time it sends thousands of SMSs. But suddenly after few days application starts facing connection issues, some time 'Negative bind response 0x00045' and some time waiting bind response. When I check from wireshark, Application constantly sends enquire line packets and receives responses for them with status 'OK'. This means that application is connected but still it is attempting for new connection. Below is code for connection management.

I call newSession method to get session for SMS sending..

 private SMPPSession newSession(BindParameter bindParam) {
        SMPPSession tmpSession = null;
        dbOperations = new DBOperations();
        Settings settings = dbOperations.getSettings();
        if (settings == null)
            logger.error("ERROR: No settings found to connect to SMSC!");
        else {
            try {
                tmpSession = new SMPPSession(remoteIpAddress, remotePort, bindParam);
                tmpSession.addSessionStateListener(new MySessionStateListener());
                tmpSession.setMessageReceiverListener(new DeliverReceiptListener());
                tmpSession.setEnquireLinkTimer(50000);
                tmpSession.setTransactionTimer(5000L);
                logger.info("New session established with " + remoteIpAddress + " on port " + remotePort + " as Transmitter");
            } catch (Exception er) {
                gateway=null;
                logger.error("Exception Occurred While making Connection with SMPP Server with IP: " + remoteIpAddress + " and port " + remotePort+" and Error is:"+er.getMessage());
            }

        }
        return tmpSession;
    }


public void reconnectAfter(final long timeInMillis) {
        final Settings settings = dbOperations.getSettings();
        if (settings == null) {
            logger.error("No settings found to connect to SMSC!");
            return;
        }
        new Thread() {
            @Override
            public void run() {
                logger.info("Schedule reconnect after " + timeInMillis + " milliseconds");
                try {
                    Thread.sleep(timeInMillis);
                } catch (InterruptedException e) {
                    logger.error(e.getMessage());
                }

                int attempt = 0;
                while (session == null || session.getSessionState().equals(SessionState.CLOSED)) {
                    try {
                        logger.info("Reconnecting attempt #" + (++attempt) + "...");
                        session = newSession(bindParam);
                    } catch (Exception e) {
                        logger.error("Failed opening Transmitter connection and bind to " + remoteIpAddress + ":" + remotePort + " ");
                        logger.error(e.getMessage());
                        // wait for a second
                        try {
                            Thread.sleep(reconnectInterval);
                        } catch (InterruptedException ee) {
                            logger.error(e.getMessage());
                        }
                    }
                }
            }
        }.start();
    }
    private class MySessionStateListener implements SessionStateListener {

        public void onStateChange(SessionState newState, SessionState oldState, Object o) {
            if (newState.equals(SessionState.OPEN)) {
                logger.info("TCP connection established with SMSC at address " + remoteIpAddress);
            }
            if (newState.equals(SessionState.BOUND_TRX)) {
                logger.info("SMPP Transceiver connection established with SMSC at address " + remoteIpAddress + " and port " + remotePort);
            }
            if (newState.equals(SessionState.CLOSED) || newState.equals(SessionState.UNBOUND)) {

                logger.error("Connection closed, either by SMSC or there is network problem");
                if(newState.equals(SessionState.CLOSED))
                    logger.error("Connection closed");
                else
                    logger.error("Connection unbound");
                logger.info("Reconnecting.......");
                reconnectAfter(reconnectInterval);
            }
        }
    }

I don't why this code retries for fresh connection when it is already connected. Any clue is appreciated.

Upvotes: 0

Views: 2750

Answers (1)

uudashr
uudashr

Reputation: 129

It seems the session still valid. Make sure there are no zombie session, if there are any then close them all. It make sure the enquire link sending stop.

Upvotes: 1

Related Questions