SoapConnection CALLS在STATIC方法中返回错误(混合)值

时间:2018-07-02 21:28:57

标签: java multithreading soap

我有一个静态方法,该方法创建SoapConnectionSoapEnvelope并以字符串形式返回soapresponse。也从静态方法中调用此方法。当我随后给他们打电话时,第二个电话正在返回第一个电话的结果。我的意思是,反应是混杂的。 它正在返回上一个调用的“应该是”的答案。这些方法是从多个线程调用的,因此我添加了synchronized关键字,但不能解决问题。例如,未分配的票证会返回用户结果...您可以在下面找到它们

下面的示例代码;

private synchronized static String getWebServiceResponse(String query) { // Action = http://tempuri.org/GetReport
        System.out.println("Creating Web Service Request");
        System.out.println(query);
        try {
            SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
            SOAPConnection soapConnection = soapConnectionFactory.createConnection();

            SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(query), SOAP_ENDPOINT_URL);
            MimeHeaders SH = soapResponse.getMimeHeaders();

            ByteArrayOutputStream out = new ByteArrayOutputStream();
            soapResponse.writeTo(out);
            String strMsg = new String(out.toByteArray(),Charset.forName("utf-8"));
            soapConnection.close();

            System.out.println("Web Service Request Is Done");
            return strMsg;
        } catch (Exception e) {
            System.err.println("Web Service Error: "+ e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    private synchronized static void createSoapEnvelope(SOAPMessage soapMessage,String query) throws SOAPException {

        SOAPPart soapPart = soapMessage.getSOAPPart();
        String myNamespace = "myNamespace";
        String myNamespaceURI = "http://www.w3.org/2001/XMLSchema";

        // SOAP Envelope
        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.setEncodingStyle("utf-8");
        envelope.addNamespaceDeclaration(myNamespace,myNamespaceURI);

        SOAPBody soapBody = envelope.getBody();
        SOAPElement soapBodyElem = soapBody.addChildElement("GetReport","","http://tempuri.org/");
        SOAPElement guidElem = soapBodyElem.addChildElement("guid");
        SOAPElement queryElem = soapBodyElem.addChildElement("reportQuery");
        guidElem.addTextNode(WS_KEY);
        queryElem.addTextNode(query);
    }
    private synchronized static SOAPMessage createSOAPRequest(String query) throws Exception {
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();

        createSoapEnvelope(soapMessage,query);

        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("etag","123asd123asd");
        headers.addHeader("Cache-Control", "max-age=0, must-revalidate");
        headers.addHeader("SOAPAction", SOAP_ACTION);
        headers.addHeader("Content-Type","text/xml; charset=utf-8");

        soapMessage.saveChanges();

        return soapMessage;
    }

static ArrayList<User> getUsers(){

        ArrayList<User> users = new ArrayList<>();
        String query = "SELECT Id,Firstname + ' ' + Lastname as 'Fullname',Email FROM Users WHERE CompanyId =89";
        try{

            String res = getWebServiceResponse(query);

            NodeList rowList = getNodeListFromResponse(Objects.requireNonNull(res));
            for(int a = 0; a < rowList.getLength(); a++){
                Node row = rowList.item(a);
                NodeList nl = row.getChildNodes();

                User u = new User();
                u.setId(Integer.parseInt(nl.item(0).getTextContent()));
                u.setFullNameProperty(nl.item(1).getTextContent());
                u.setEmail(nl.item(2).getTextContent());
                users.add(u);
            }

        }catch (Exception ex){
            ex.printStackTrace();
            NotificationUtils.showError("Get Users error\n"+ex.getMessage()+ "\n" + Arrays.toString(ex.getStackTrace()));
        }

        return users;
    }

static ArrayList<Ticket> getUnassignedTickets(int lastKnownTicketId){

        ArrayList<Ticket> tickets = new ArrayList<>();

        int lastTicketNo = lastKnownTicketId == -1 ? MINIMUM_TICKETID : lastKnownTicketId;
        String query =  "SELECT T.Id,T.Subject,T.InitialId as 'TicketNo',U.Firstname + ' ' + U.Lastname as 'CreatorName',C.Name as 'CompanyName' FROM Tickets T " +
                        "INNER JOIN Users U ON U.Id = T.SenderUserId " +
                        "INNER JOIN Companies C ON C.Id = U.CompanyId " +
                        "WHERE T.Id >"+lastTicketNo+" AND T.ProductId = "+GeneralUtils.getSettings().getProductId()+"  " +
                        "AND T.ResponsibleCompanyId = 89 AND T.AssignedUserId = 0 AND T.Status !=4 " +
                        "ORDER BY T.Id";

        try {

            String res = getWebServiceResponse(query);

            NodeList rowList = getNodeListFromResponse(Objects.requireNonNull(res));
            for (int a = 0; a < rowList.getLength(); a++) {
                Node row = rowList.item(a);
                NodeList nl = row.getChildNodes();

                Ticket t = new Ticket();
                for(int b = 0; b <nl.getLength(); b++) {
                    Node n = nl.item(b);

                    if (n.getNodeName().equals("Id"))
                        t.setId(Integer.parseInt(n.getTextContent()));
                    else if (n.getNodeName().equals("Subject"))
                        t.setSubject(StringEscapeUtils.unescapeHtml4(n.getTextContent().trim()));
                    else if (n.getNodeName().equals("TicketNo"))
                        t.setTicketNo(n.getTextContent());
                    else if (n.getNodeName().equals("CreatorName"))
                        t.setCreatorName(n.getTextContent());
                    else if (n.getNodeName().equals("CompanyName"))
                        t.setCompanyName(n.getTextContent());
                }

                if(t.getId() != -1)
                    tickets.add(t);
            }

            return tickets;

        } catch (Exception e) {
            e.printStackTrace();
            NotificationUtils.showError("Unassigned error\n"+e.getMessage()+ "\n" + Arrays.toString(e.getStackTrace()));
        }

        return  tickets;
    }

private static NodeList getNodeListFromResponse(String soapResponse) throws IOException, SOAPException {

        MessageFactory factory = MessageFactory.newInstance();
        SOAPMessage message = factory.createMessage(
                new MimeHeaders(),
                new ByteArrayInputStream(soapResponse.getBytes(Charset
                        .forName("UTF-8"))));

        return message.getSOAPBody().getElementsByTagName(ROW_KEY_NAME);
    }

0 个答案:

没有答案