Hi All,
I am getting the problem in a socket program as the socket is not able to receive the first request after long ideal time(one day). I have sent the request to the socket program from simulator program but it did not get the request for first time but after that it is receiving all the requests.
This Socket program is running in glassfish server in Windows 2008 Operating system.
We are using a thread to start and listern the socket to receive the request and we did not configured any time out for the socket.
The Below Method will be called in the Glassfish Server Startup itself. This method is written in the application's Life Cylcle listener class so this will create the socket and the thread will call the listern() method in SocketListener class to start to listen for request.
ServerSocket srvsocket = null; SocketConnector socConn = null; public void openSocketListener() { int port = 6151; int poolSize = 15; try { srvsocket = new ServerSocket(port); SocketConnector socConn = SocketConnector .getInstance(srvsocket); socConn.initiate(poolSize); } } catch (Exception e) { LOG.error("Exception in OpenSocketListener : " + e.getMessage()); } }
================================================== ==========================
Below is the Socket program that we are using to receive the request from the Front End system.package com.test.common.Connector; import com.test.we.common.utility.MyLogger; import java.net.ServerSocket; public class SocketConnector implements Runnable { /** Logger Object for loMyng activity */ private static MyLogger log = (MyLogger) MyLogger .getLogger(SocketConnector.class.getName()); private Thread connectorThread; private ServerSocket mSocketServer; private SocketListener MySocket; private int capacity = 0; /** * This method sets the Server Socket * * @param socketServer */ public SocketConnector(ServerSocket socketServer) { mSocketServer = socketServer; } public boolean initiate(int poolsize) throws Exception { if (log.isDebugEnabled()) { log.debug("Creation of pool of Threads"); } this.setCapacity(poolsize); connectorThread = new Thread(this); connectorThread.start(); return true; } /** * Sets the thread pool capacity * * @param capacity - * Thread pool capacity */ public void setCapacity(int capacity) { this.capacity = capacity; } /** * Sets the time of inactivity, after which the connection to the client * will be closed and thread returned to the thread pool. * * @param intTimeout - * the inactivity timeout period in seconds. */ public void setTimeOut(int intTimeout) { } /** * Returns an instance of the socket connector * * @param socketServer * @return */ public static synchronized SocketConnector getInstance( ServerSocket socketServer) { return new SocketConnector(socketServer); } public void run() { MySocket = new SocketListener(mSocketServer, this.capacity); MySocket.listen(); } }
package com.test.common.Connector; import com.test.weMy.common.exception.MyException; import com.test.weMy.common.utility.MyConstants; import com.test.weMy.common.utility.MyLogger; import com.test.weMy.common.utility.MyThread; import com.test.weMy.common.utility.MyThreadPool; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; import org.apache.commons.lang.StringUtils; public class SocketListener { /** Logger Object for loMyng activity */ private static MyLogger log = (MyLogger) MyLogger .getLogger(SocketListener.class.getName()); /** ServerSocket Initialisation */ private ServerSocket serverSocket; /** Socket to read the data */ //private Socket socket = null; public static Socket socket = null; /** Pool initialization */ private static MyThreadPool pool = null; public SocketListener() { } public SocketListener(ServerSocket srvSocket) { serverSocket = srvSocket; } /** * Constructor for the socket Initializes a server socket Initializes a * Thread Pool * * @param srvSocket * @param capacity */ public SocketListener(ServerSocket srvSocket, int capacity) { serverSocket = srvSocket; pool = new MyThreadPool(capacity, new MyThread[capacity]); } /** * This is the method to listen to the sockets It will initiate a thread * once it receives data in the socket And send to XMLProcessor for My * processing */ public void listen() { log.debug("Listen Method in My SocketListener Ok"); while (!serverSocket.isClosed()) { try { socket = serverSocket.accept(); BufferedReader reader = new BufferedReader( new InputStreamReader(socket.getInputStream())); StringBuffer strBuffMessage = new StringBuffer(); String temMessage = null; int i = 0; while ((temMessage = reader.readLine()) != null) { i++; if (!StringUtils.isEmpty(temMessage)) { strBuffMessage.append(temMessage); } } String message = strBuffMessage.toString(); log.debug("FE String is >>"+message); MyThread Mythread = (MyThread) pool.getThread(); log.debug("Thread Received"); Mythread.init(message, MyConstants.FE_TYPE_SOCKET); Mythread.start(); } catch (IOException ioe) { if(!serverSocket.isClosed()){ log.error("IO Exception in writing to Socket",ioe); } } catch (MyException ex) { log.error("Problem of management of the pool of threads." + ex.getMessage()); } } try { pool.close(); } catch (MyException e) { log.fatal("Exception in closing Thread Pool: " + e.getMessage()); } } }
Here my problem is, I am not able to receive the first request after I put the glassfish sever to ideal for one or two days.
I mean, I did not receive any log that is available in the listen() method and the process in not continued for the first request only, I am able to receive request and it is processed further from the second request.
Kindly help me to resolve this issue, because it is very critical to me.
Thanks in Advance.
Regards,
Venkatesan.J