美文网首页
05-并发调用WS

05-并发调用WS

作者: XAbo | 来源:发表于2021-06-09 23:43 被阅读0次

客户端:

package com.examples.temp;
import java.nio.charset.Charset;
import java.util.concurrent.CountDownLatch;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import javax.annotation.PostConstruct;

public class HttpClientCallSoapUtil {


    public static final int THREAD_NUM = 100;
    private static long startTime = 0L;
    static int socketTimeout = 30000;// 请求超时时间
    static int connectTimeout = 30000;// 传输超时时间
    static Logger logger = Logger.getLogger(HttpClientCallSoapUtil.class);

    public static String doPostSoap1_1(String postUrl, String soapXml,String soapAction) {
        String retStr = "";
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
        HttpPost httpPost = new HttpPost(postUrl);
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build();
        httpPost.setConfig(requestConfig);
        try {
            httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
            httpPost.setHeader("SOAPAction", soapAction);
            StringEntity data = new StringEntity(soapXml,Charset.forName("UTF-8"));
            httpPost.setEntity(data);
            CloseableHttpResponse response = closeableHttpClient.execute(httpPost);
            HttpEntity httpEntity = response.getEntity();
            if (httpEntity != null) {
                retStr = EntityUtils.toString(httpEntity, "UTF-8");
                logger.info("response:" + retStr);
            }
            closeableHttpClient.close();
        } catch (Exception e) {
            logger.error("exception in doPostSoap1_1", e);
        }
        return retStr;
    }
    @PostConstruct
    public void init() {
        try {

            startTime = System.currentTimeMillis();
            System.out.println("CountDownLatch started at: " + startTime);
            // 初始化计数器为1
            CountDownLatch countDownLatch = new CountDownLatch(1);
            for (int i = 0; i < THREAD_NUM; i ++) {
                new Thread(new Run(countDownLatch)).start();
            }
            // 启动多个线程
            countDownLatch.countDown();
        } catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
    private class Run implements Runnable {
        private final CountDownLatch startLatch;
        public Run(CountDownLatch startLatch) {
            this.startLatch = startLatch;
        }

        @Override
        public void run() {
            try {
                // 线程等待
                startLatch.await();

                String orderSoapXml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservice\">\n" +
                        "   <soapenv:Header/>\n" +
                        "   <soapenv:Body>\n" +
                        "      <web:saveOrUpdatePatientInfo>\n" +
                        "         <!--Optional:-->\n" +
                        "         <requestInfo>\n" +
                        "            <requestHeader>\n" +
                        "               <appid>JBKB4H4AVI9MM26OIE91594890246643</appid>\n" +
                        "               <timestamp>1610004440295</timestamp>\n" +
                        "               <sign>525C82BECF20D8435F60AE88C68D3103</sign>\n" +
                        "            </requestHeader>\n" +
                        "            <requestBody>\n" +
                        "               <handleType>1</handleType>\n" +
                        "               <patientId></patientId>\n" +
                        "               <patientName>阿拉伯</patientName>\n" +
                        "               <gender>2</gender>\n" +
                        "               <birthday/>\n" +
                        "               <age>20岁</age>\n" +
                        "               <phone>13703504526</phone>\n" +
                        "               <maritalStatusCode/>\n" +
                        "               <politicalNation/>\n" +
                        "               <raceCode/>\n" +
                        "               <occupationCode/>\n" +
                        "               <patientType>1</patientType>\n" +
                        "               <bloodABO/>\n" +
                        "               <bloodRH/>\n" +
                        "               <fax/>\n" +
                        "               <email/>\n" +
                        "               <workUnit/>\n" +
                        "               <nativePlace/>\n" +
                        "               <birthplace/>\n" +
                        "               <linkmanName/>\n" +
                        "               <relationship/>\n" +
                        "               <linkmanPhone/>\n" +
                        "               <educationLevel/>\n" +
                        "               <healthCardNo/>\n" +
                        "               <allergyFlag/>\n" +
                        "               <allergy/>\n" +
                        "               <infectiousFlag/>\n" +
                        "               <infectious/>\n" +
                        "               <chargeTypeCode/>\n" +
                        "               <chargeTypeName/>\n" +
                        "               <healthRecordMgtName/>\n" +
                        "               <healthRecordNo/>\n" +
                        "               <healthRcdMgtGrpCode/>\n" +
                        "               <healthRcdCreateDate/>\n" +
                        "               <workStartDate/>\n" +
                        "               <nativePlaceName/>\n" +
                        "               <inpNo></inpNo>\n" +
                        "               <inpFreq></inpFreq>\n" +
                        "               <chargeCardNo/>\n" +
                        "               <idNoList>\n" +
                        "                  <idNoInfo>\n" +
                        "                     <idType>01</idType>\n" +
                        "                     <idNo></idNo>\n" +
                        "                  </idNoInfo>\n" +
                        "               </idNoList>\n" +
                        "               <addressList/>\n" +
                        "            </requestBody>\n" +
                        "         </requestInfo>\n" +
                        "      </web:saveOrUpdatePatientInfo>\n" +
                        "   </soapenv:Body>\n" +
                        "</soapenv:Envelope>";
                String postUrl = "http://192.168.202.60:8080/platform/ws/patient?wsdl";


                doPostSoap1_1(postUrl, orderSoapXml, "");

                long endTime = System.currentTimeMillis();
                System.out.println(Thread.currentThread().getName() + " ended at: " + endTime + ", cost: " + (endTime - startTime) + " ms.");
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    public static void main(String[] args) {
        HttpClientCallSoapUtil apiTest = new HttpClientCallSoapUtil();
        apiTest.init();
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example</groupId>
  <artifactId>MyDemo_05_SSM</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>MyDemo_05_SSM Maven Webapp</name>
  <url>http://www.example.com</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.5.6</version>
    </dependency>
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpcore</artifactId>
      <version>4.4.10</version>
    </dependency>

    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>

    <dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-client</artifactId>
      <version>5.15.9</version>
    </dependency>
    <dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-pool</artifactId>
      <version>5.15.9</version>
    </dependency>
    <dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-broker</artifactId>
      <version>5.15.9</version>
    </dependency>
  </dependencies>

  <build>

    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
        <filtering>true</filtering>
      </resource>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
        <filtering>true</filtering>
      </resource>
    </resources>
  </build>
</project>

相关文章

  • 05-并发调用WS

    客户端: pom.xml

  • Java并发编程脑图

    01-Java内存模型 02-并发基础 03-锁 04-并发工具类 05-其他 06-Java并发集合 07-at...

  • WS 并发问题

  • websocket html

    var ws = new WebSocket('ws://127.0.0.1:8000/ws'); ws.onop...

  • 技术文章

    java中一个汉字占几个字节 数据库并发操作带来的问题 concat、concat_ws、group_concat...

  • websocket

    var ws=new WebSocket('ws://192.168.2.11:8181');ws.onmessa...

  • Js WebSocket

    var ws = new WebSocket("ws://localhost:7272[ws://localhos...

  • Axios请求并发限制

    标签 NodeJS并发请求,并行请求,QPS限制,Axios并发限制,Axios并发请求 背景 由于调用第三方服务...

  • python进阶-05-并发编程

    1 概念 概念 并发即同时执行真正的并发是在多核CPU上执行,任务数多于核心数则多核轮流执行 模拟并发 2.进程创...

  • 第05天C语言(00):笔记总结

    01-函数-基本概念 02-函数-定义格式 03-函数-调用过程 04-函数-注意点 05-函数-练习 06-函数...

网友评论

      本文标题:05-并发调用WS

      本文链接:https://www.haomeiwen.com/subject/kdcfeltx.html