java - 线程响应在 weblogic 部署的应用程序中交换

标签 java multithreading sockets weblogic

我通过删除我的帖子打开了这个问题,其中我错误地提出了问题,包括标题,并且提供的信息不多。我的问题是线程请求和响应在某些情况下会交换。

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

从日志中我发现线程正在交换..下面是 weblogic 日志 假设两个线程(7,8)处理两个portId (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 的映射值即将为空,则我们将抛出应用程序特定的错误。 下面是代码。如何避免这种情况。

我正在提取实时生产代码和日志。

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从EjbApplication(JAR文件)读取信息

请您帮助我,这个问题的根本原因是什么以及如何解决它。 让我知道还需要更多信息。

最佳答案

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

关于java - 线程响应在 weblogic 部署的应用程序中交换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37222266/

相关文章:

Java IntToBinary 应用程序 : Exception in thread "main" java. lang.IllegalStateException:扫描仪已关闭

c++ - std::mutex 和 condition_variable 无法访问类内的私有(private)成员

java - 需要可靠/持久的出站套接字,选项?

java - 套接字在将 json 对象从 Java 传输到 Python 时被阻塞

python - 带有pygame和套接字的“SyntaxError: unexpected EOF while parsing”

java - 在注释中使用属性值?

java - Java中的FIFO类

java - hibernate :列不存在

c++ - 为什么 Boost 原子使用中的多生产者队列是无等待的

java - 用线程显示动画