how to fix SocketTimeoutException when receiving datagram packet

Viewed 60

i am using the jsf and if i want to add a configuration in the beans it need to send in the form of xml string to other module which creates the data but i am facing socket time out issue for the socket response ,i mean i am not able to receive response.

and this my code

    private void send(Node n) throws ModuleException {
            try {

                    connection = new DatagramSocket();

                    StringWriter sw = new StringWriter();
                    Transformer serializer = TransformerFactory.newInstance().newTransformer();
                    serializer.transform(new DOMSource(n), new StreamResult(sw));

                    InetAddress host = InetAddress.getByName(LOCAL_HOST);
                    DatagramPacket request =
                            new DatagramPacket(sw.toString().getBytes(),  sw.toString().length(), host, LOCAL_PORT);

                    if (log.isDebugEnabled())
                            log.debug("Sending the request to policy engine :"+sw.toString());

                    connection.send(request);

                    if (log.isDebugEnabled())
                            log.debug("Request sent to policy engine : OK");

                    connection.setSoTimeout(5000);


                    byte[] buffer = new byte[1024];
                    DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
                    connection.receive(reply);

                    String replyMessage = new String(reply.getData()).trim();


                    if (replyMessage.indexOf("code=\"0\"") < 0){
                            String errStr = "Response rejected the config:"+parseMessage(replyMessage);
                            if (log.isInfoEnabled())
                                    log.info(errStr);
                            throw new IOException(errStr);
                    }

            }catch(SocketTimeoutException e){
                    log.error("Unable to connect to  engine", e);
                    log.error("Unable to connect to  engine", e);
                    throw new ModuleException("policy is busy");
            }catch (Exception e){
                    log.error("Unable to send data toengine", e);
                    throw new ModuleException("Unable to send the information to the engine");
            }finally {if(connection != null) connection.close();}
    }

and this is the root cause

java.net.SocketTimeoutException: Receive timed out

at java.net.PlainDatagramSocketImpl.receive0(Native Method)

at java.net.PlainDatagramSocketImpl.receive(PlainDatagramSocketImpl.java:145)
at java.net.DatagramSocket.receive(DatagramSocket.java:725)
at com.redshift.mgmt.resource.PolicyEngineConnection.send(PolicyEngineConnection.java:98)
at com.redshift.mgmt.resource.PolicyEngineConnection.send(PolicyEngineConnection.java:67)
at com.redshift.mgmt.module.PolicyEngine.sendNetworkService(PolicyEngine.java:360)
at com.redshift.mgmt.module.PolicyEngine.addNetworkService(PolicyEngine.java:338)
at com.redshift.mgmt.beans.NetworkServiceListBean.saveService(NetworkServiceListBean.java:424)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

Please help me how to fix this exception

Thank you

0 Answers
Related