I opened this question by deleting the my post, where I asked question wrongly including title and provided not much information.My problem is thread request and responses are swapping in some cases.
Below are the application weblogic logs and the code
From the logs I found that threads are swapping.. Below are the weblogic logs say two threads(7,8) processing performX() for two portIds (1234,4567)
<DEBUG> 2016-05-11 16:55:45,319 [ExecuteThread: '7'----] processing data for port id = 1234
<DEBUG> 2016-05-11 16:XX:YY,319 [ExecuteThread: '8'----] processing data for port id = 4567
<DEBUG> 2016-05-11 16:XX:YY [ExecuteThread: '7'----] ResponseQueue :: getResponse() :: ResponseQueue456
<DEBUG> 2016-05-11 16:XX:YY [ExecuteThread: '7'----] ResponseQueue :: getResponse() :: Reading Data from stream :: ResponseQueue456
<DEBUG> 2016-05-11 16:XX:YY [ExecuteThread: '7'----] ResponseQueue :: getResponse() :: Reading Data from stream completed :: ResponseQueue456
<DEBUG> 2016-05-11 16:XX:YY [ExecuteThread: '7'----]:: ReturnXML=<xml>---<order_number>4567</order_number> ---</xml>
<DEBUG> 2016-05-11 16:XX:YY [ExecuteThread: '7'----]:: Adding ordernumber as Key = 4567 value = ReturnXmlObjet1234
<DEBUG> 2016-05-11 16:XX:YY [ExecuteThread: '7'----]:: Getting object of port Key = 1234 value = null
<DEBUG> 2016-05-11 16:XX:YY [ExecuteThread: '8'----] ResponseQueue :: getResponse() :: ResponseQueue456
<DEBUG> 2016-05-11 16:XX:YY [ExecuteThread: '8'----] ResponseQueue :: getResponse() :: Reading Data from stream :: ResponseQueue456
<DEBUG> 2016-05-11 16:XX:YY [ExecuteThread: '8'----] ResponseQueue :: getResponse() :: Reading Data from stream completed :: ResponseQueue456
<DEBUG> 2016-05-11 16:XX:YY [ExecuteThread: '8'----]:: ReturnXML=<xml>---<order_number>1234</order_number> ---</xml>
<DEBUG> 2016-05-11 16:XX:YY [ExecuteThread: '8'----]:: Adding ordernumber as Key = 1234 value = ReturnXmlObjet4567
<DEBUG> 2016-05-11 16:XX:YY [ExecuteThread: '8'----]:: Getting object of port Key = 4567 value = ReturnXmlObjet1234
If you see clearly thread 7 and thread 8 are swapping , as the map value for thread 7 is coming null, we are throwing applicationspecific error. Below is the code. How to avoid this scenario.
I am abstracting the real time production code and logs.
Class A{
----
B bobj = B.getInstance()
Map map = new HashMap();
public void performX(String portid){
logger.debug("Processing data for portId = "+ portId);
returnXml = bobj.getResponse(); // bobj is a singleton
logger.debug("returnXML ="+ retunrXML);
ReturnXmlObjet r_obj = convertToObject(returnXMl);
// retunrXml contain a field orderNumber which must be same as
// portid method parameter
if(r_obj != null){
logger.debug("Adding ordernumber as Key = "+ r_obj.getOrderNumber()+ "Value is =" + r_obj);
map.put(r_obj.getOrderNumber,r_obj);
}
r_obj = map.get(portid);
logger.debug("Getting object of port Key = "+ r_obj.getOrderNumber()+ "Value is =" + r_obj);
if(r_obj == null){
throw new ApplicationSpecificException("Not able to get Response");
}
}
}
Class B(){
B instance = null;
public static synchronized BgetInstance() throws AboxRetryException {
if (instance == null) {
instance = new B();
}
return instance;
}
public synchronized String getResponse() {
logger.debug(" ResponseQueue :: getResponse() :: "+ this);
String returnXML = null;
String strResponseACK;
InstantLinkXML ilXML = new InstantLinkXML();
try {
logger.debug(" ResponseQueue :: Reading Data from stream :: "+ this);
returnXML = in.readLine();
logger.debug(" ResponseQueue :: Reading Data completed from stream :: "+ this);
} catch (IOException ioe) {
logger.error("Failure getting Response");
} catch(Exception e){
logger.error(e.getMessage());
}
if (returnXML != null) {
// Send ACK
strResponseACK = ilXML.getxxx();
out.println(strResponseACK);
}
//code to convert XML to Java object.. ReturnObject has a property port_db
return returnXML;
}
Below is the code for *in* object initialization. This login() method is called in the class constructor where getResponsed() method is written
public String logIn() {
String returnXML = null;
try {
String strUser = ConfigItemsAccess.getConfigItems("o2.xxx.user");
String strPass = ConfigItemsAccess.getConfigItems("o2.xxx.password");
String strServer = ConfigItemsAccess.getConfigItems("o2.xxx.server");
int iPort = Integer.parseInt(ConfigItemsAccess.getConfigItems("o2.xxx.respport"));
logger.debug("logIN :: strUser="+strUser+" strPass="+strPass+" strServer="+strServer+ " iport = "+ iPort );
String strLoginRequest;
String strLoginAck;
InstantLinkXML ilXML = new InstantLinkXML();
try {
XXXSocket( Just a SOCKET object abstracted according to application) = new Socket(strServer, iPort);
xxxSocket.setKeepAlive(true); //new
out = new PrintWriter(xxxSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(xxxSocket.getInputStream()));
} catch (UnknownHostException e) {
logger.error("Cannot connect to host: " + e.getMessage());
} catch (IOException e) {
logger.error("Couldn't get I/O for the connection to:" + strServer + ":" + iPort);
// Connect to Secondary Server
strServer = ConfigItemsAccess.getConfigItems("o2.xxx.server.backup");
try {
logger.debug("Connecting to Secondary Server: " + strServer);
xxxSocket = new Socket(strServer, iPort);
xxxSocket.setKeepAlive(true); //new
out = new PrintWriter(xxxSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(xxxSocket.getInputStream()));
} catch (UnknownHostException e2) {
logger.error("Cannot connect to host: " + e2.getMessage());
}
}
} catch (IOException e2) {
logger.error("Couldn't get I/O for the Secondary connection to:" + strServer + ":" + iPort);
}
} catch(Exception e){
logger.error(e.getMessage());
}
strLoginRequest = ilXML.getLogInRequest(strUser, strPass);
out.println(strLoginRequest);
try {
strLoginAck = in.readLine();
returnXML = strLoginAck;
} catch (IOException ioe) {
logger.error("Failure getting Comptel Ack back");
} catch(Exception e){
logger.error(e.getMessage());
}
}
catch (FinderException Ex) {
logger.error("FinderException trying to get o2.xxx from CONFIGITEMS\n" + Ex.getMessage());
throw new java.lang.NullPointerException("Exception trying to get config items");
}
catch (Exception e){
logger.error(e.getMessage());
}
return returnXML;
}
ilXML.getLogInRequest(String strUser, String strPass){
return ("<Message version=\"SAS3\">\n" + " <LOGIN\n" + " LOGIN=\""
+ strUser + "\"\n" + " PASSWORD=\"" + strPassword + "\"/>\n" + "</Message>\n");
}
Socket Read the information from an EjbApplication(JAR file)
Please can you help me, what might be the root causes for this issue and how can I fix it. Let me know still need more information.
returnXML = in.readLine();
. I ask again, where is the (real) evidence of "swapping"? – Stephen C