Handling java.net.SocketException - java

I have a program which sometimes encounters the errorjava.net.SocketException. Is there a way I can have the client program execute some code if (and only if) it encounters this error in order to "deal with" the error? The full error is
java.net.SocketException: Software caused connection abort: socket write error
Here's a summary of what I have now.
public void run() {
try {
//some code that causes the SocketException
}
catch (SocketException e) {
System.out.println("I recognize the SocketException");
}
}
However despite the error it still won't print the line.
Here is the full error
java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(Unknown Source)
at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(Unknown Source)
at java.io.ObjectOutputStream.writeNonProxyDesc(Unknown Source)
at java.io.ObjectOutputStream.writeClassDesc(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeFatalException(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at (...).java:61)
at (...).java:164)

It depends on whether the client or the server is the one throwing the exception.
Either way, you should just be able to add a try-catch block around the code.
try {
//code
} catch (SocketException e) {
// Handle the error
}
If the server is throwing the error and you want the client to handle the situation, the client should be throwing an exception due to the loss of connection (this is what this exception looks like). Alternatively, if the server throws an error but doesn't crash, just have the server send some signal to the client that an error occurred.
Either way, with a little more information, I could give you a better resposne.

Related

JMX - how to monitor a local and remote cpu usage of an application

Host : serverhost.example.com / 127.0.0.1
Port : 8082
API/Application : Google Chrome / Tomcat deployed application
Monitor : CPU = 35%
Memory = 30%
this is my code snippet :
String url = "service:jmx:rmi:///jndi/rmi://127.0.0.1:8082/jmxrmi";
JMXConnector jmxc = null;
try {
JMXServiceURL jmxSvcUrl = new JMXServiceURL(url);
System.out.println("~~~~~~~~~~~1~~~~~~~~~~~~~~");
jmxc = JMXConnectorFactory.connect(jmxSvcUrl, null);
System.out.println("~~~~~~~~~~~2~~~~~~~~~~~~~~");
MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
String domains[] = mbsc.getDomains();
Arrays.sort(domains);
for (String domain : domains) {
System.out.println( domain );
}
System.out.println("~~~~~~~~~~~3~~~~~~~~~~~~~~");
}catch(Exception ex){
ex.printStackTrace();
}finally {
if( jmxc != null )
try{jmxc.close();}catch(Exception ex){;}
}
and i got this error
java.io.IOException: Failed to retrieve RMIServer stub: javax.naming.CommunicationException [Root exception is java.rmi.ConnectIOException: error during JRMP connection establishment; nested exception is:
java.net.SocketTimeoutException: Read timed out]
at javax.management.remote.rmi.RMIConnector.connect(Unknown Source)
at javax.management.remote.JMXConnectorFactory.connect(Unknown Source)
at com.ms3.health.core.ResourceManagement.jmxSvcUrl(ResourceManagement.java:137)
at com.ms3.health.core.ResourceManagement.main(ResourceManagement.java:201)
Caused by: javax.naming.CommunicationException [Root exception is java.rmi.ConnectIOException: error during JRMP connection establishment; nested exception is:
java.net.SocketTimeoutException: Read timed out]
at com.sun.jndi.rmi.registry.RegistryContext.lookup(Unknown Source)
at com.sun.jndi.toolkit.url.GenericURLContext.lookup(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at javax.management.remote.rmi.RMIConnector.findRMIServerJNDI(Unknown Source)
at javax.management.remote.rmi.RMIConnector.findRMIServer(Unknown Source)
... 4 more
Caused by: java.rmi.ConnectIOException: error during JRMP connection establishment; nested exception is:
java.net.SocketTimeoutException: Read timed out
at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
at sun.rmi.server.UnicastRef.newCall(Unknown Source)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
... 9 more
Caused by: java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at java.io.DataInputStream.readByte(Unknown Source)
... 13 more
please see my code snippet for more details. thanks and more power to everyone.
please let me understand what cause of my issues and how to deal with it.
please see my code snippet for more details. thanks and more power to everyone.
please let me understand what cause of my issues and how to deal with it.

When javax.xml.ws.soap.SOAPFaultException happen?

My code to call to a port name getDUNSProfile() looks like this:
public class LookupRequestProcessor
{
protected GetCleanseMatchResponse sendRequest(Request request) throws Exception_Exception, GetDUNSProfileFault, PayloadException
{
return DNBPortUtil.getDunsService().getDUNSProfile();
}
}
And I receive an exception as below (Updated with full stacktrace)
java.lang.Error: javax.xml.ws.soap.SOAPFaultException: Error in operation:
at imx.svb.module.getdunsprofile.LookupRequestProcessor.doWork(LookupRequestProcessor.java:478)
at imx.svb.TemplateDatablockProcessor.run(TemplateDatablockProcessor.java:63)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: javax.xml.ws.soap.SOAPFaultException: Error in operation:
at com.sun.xml.ws.fault.SOAP11Fault.getProtocolException(SOAP11Fault.java:197)
at com.sun.xml.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:130)
at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:125)
at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:95)
at com.sun.xml.ws.client.sei.SEIStub.invoke(SEIStub.java:136)
at com.sun.proxy.$Proxy97.getDUNSProfile(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at weblogic.wsee.jaxws.spi.ClientInstanceInvocationHandler.invoke(ClientInstanceInvocationHandler.java:84)
at com.sun.proxy.$Proxy98.getDUNSProfile(Unknown Source)
at imx.svb.module.getdunsprofile.LookupRequestProcessor.sendRequest(LookupRequestProcessor.java:258)
at imx.svb.module.getdunsprofile.LookupRequestProcessor.doWork(LookupRequestProcessor.java:472)
Does this mean I called successfully to the port? Or still not reaching it? Is the message Error in operation from exception returned by the The WS server?
Chances are you have reached the Port. Referencing the documentation of the Fault and this thread on code ranch seems to suggest that a fault is being thrown from the provider of the service. In your code can you add a block to catch the Exception and get the SOAPFault being thrown when invoked. If the fault is well handled , it should ideally give you a reason / code as to why the exception is happening.

not able to make connection using javamail api

I am trying to make connection to the mailserver using java mail api, with user loginId password and the host name. But if any one of the id fails to make connection then the following ids also fails to authorize even if they have correct credentials.
Do I've to close the store values after the loop.
sessions = Session.getDefaultInstance(properties);
store = sessions.getStore(emailAccType);
store.connect(emailHost,emailId, emailPwd);
What i need to close in finally block of try/catch.
for the following error.
javax.mail.MessagingException: Connect failed;
nested exception is:
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:161)
at javax.mail.Service.connect(Service.java:288)
at javax.mail.Service.connect(Service.java:169)
at com.scheduler.utils.QuartzImplementation.<init>(QuartzImplementation.java:77)
at com.scheduler.utils.SchedulerRedirect.execute(SchedulerRedirect.java:31)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:529)
Caused by: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
at com.sun.net.ssl.internal.ssl.InputRecord.handleUnknownRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.InputRecord.read(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readDataRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.AppInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at java.io.DataInputStream.readLine(Unknown Source)
at com.sun.mail.pop3.Protocol.simpleCommand(Protocol.java:360)
at com.sun.mail.pop3.Protocol.<init>(Protocol.java:104)
at com.sun.mail.pop3.POP3Store.getPort(POP3Store.java:214)
at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:157)
for the first time it's executing but if one fails then this problem arise.
Thanks in advance
Refer the answer to this question
Please ensure you've read the java mail API FAQs completely.

Apache CXF Could not send message

im having a random error of could not send message. sometimes there's no error.
what might be the cause?? thanks.
Mar 06, 2014 6:11:13 AM org.apache.cxf.phase.PhaseInterceptorChain doDefaultLogging
WARNING: Interceptor for {XXX }SoapApiServiceHttpEndpoint#{XXX}createSession has thrown exception, unwin
ding now
org.apache.cxf.interceptor.Fault: Could not send Message.
at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:64)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:262)
at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:531)
Caused by: java.net.SocketException: SocketException invoking : Connection reset
at sun.reflect.GeneratedConstructorAccessor2332.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at sun.security.ssl.InputRecord.readFully(Unknown Source)
Connection reset means that the client abnormally closed the connection and then cxf cannot read or write any further data to the stream. Take a look at following answers for more information:java.net.SocketException: Connection reset

When is java.io.IOException: Connection reset by peer thrown with Netty?

When I send a request to a webserver using netty. I get the following exception. What can cause this exception ?
java.io.IOException: Connection reset by peer
at sun.nio.ch.FileDispatcherImpl.read0(Native Method) ~[na:1.7.0_25]
at sun.nio.ch.SocketDispatcher.read(Unknown Source) ~[na:1.7.0_25]
at sun.nio.ch.IOUtil.readIntoNativeBuffer(Unknown Source) ~[na:1.7.0_25]
at sun.nio.ch.IOUtil.read(Unknown Source) ~[na:1.7.0_25]
at sun.nio.ch.SocketChannelImpl.read(Unknown Source) ~[na:1.7.0_25]
at io.netty.buffer.UnpooledHeapByteBuf.setBytes(UnpooledHeapByteBuf.java:237) ~[netty-all-4.0.6.Final.jar:na]
at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:867) ~[netty-all-4.0.6.Final.jar:na]
at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:227) ~[netty-all-4.0.6.Final.jar:na]
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:87) ~[netty-all-4.0.6.Final.jar:na]
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:497) ~[netty-all-4.0.6.Final.jar:na]
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:465) ~[netty-all-4.0.6.Final.jar:na]
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:359) ~[netty-all-4.0.6.Final.jar:na]
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:101) ~[netty-all-4.0.6.Final.jar:na]
at java.lang.Thread.run(Unknown Source) ~[na:1.7.0_25]
The usual cause of this error is that you have written to a connection which had already been closed by the other end. In other words, an application protocol error. There are other causes, but this is the most common.
NB Netty has nothing to do with it.
Try check and catch exception before read. It helped me rid of that exception.
// Attempt to read off the channel
int numRead;
try {
numRead = socketChannel.read(this.readBuffer);
} catch (IOException e) {
// The remote forcibly closed the connection, cancel
// the selection key and close the channel.
key.cancel();
socketChannel.close();
return;
}

Categories

Resources