线程响应在weblogic部署的应用程序中进行交换

时间:2016-05-14 03:42:18

标签: java multithreading sockets weblogic

我通过删除我的帖子打开了这个问题,在那里我错误地提出了问题,包括标题并没有提供太多信息。我的问题是线程请求,并且在某些情况下回复交换。

以下是应用程序weblogic日志和代码

从日志中我发现线程正在交换..下面是weblogic日志 说两个线程(7,8)处理两个portIds(1234,4567)

的performX()
<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

如果你看到线程7和线程8交换清楚,因为线程7的映射值为null,我们将抛出特定于应用程序的错误。 下面是代码。如何避免这种情况。

我正在抽象实时生产代码和日志。

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");

    }

套接字从EjbApplication(JAR文件)中读取信息

请帮助我,这个问题可能是根本原因,我该如何解决? 让我知道还需要更多信息。

1 个答案:

答案 0 :(得分:1)

看起来returnXml是在方法之外声明的。当你调用getResponse()时,你会覆盖之前的任何内容。