我有以下類,其中spring爲類創建對象。我已經創建WebClient使用CXF進行http請求。我想做2個http調用,我想在2個線程中執行,以便我不必調用該方法兩次。如何在多線程中執行這一行java
@Service
public class SalesManServiceWebClient {
@Value("${vehicle.api.url}")
private String vehicleUrl;
@Value("${customer.api.url}")
private String customerUrl;
private static final Logger LOG = LoggerFactory.getLogger(SalesManServiceWebClient.class);
public List<String> getListOfDmsId(String market,STouchServiceEnum stouchService)
{
List<String> dmsIdList= new ArrayList<>();
LOG.info("---Entering getListOfDmsId webClientService---");
LOG.info("customerUrl:"+customerUrl);
final String url = getServiceUrl(stouchService);
final List<Object> providers = new ArrayList<Object>();
providers.add(new JacksonJsonProvider());
//i want this to be execute in thread
CustomerServiceProxy proxy = JAXRSClientFactory.create(url, CustomerServiceProxy.class, providers);
Client client = WebClient.client(proxy);
client.type(MediaType.APPLICATION_JSON);
//client.accept(MediaType.APPLICATION_JSON);
client.header("ST_USER", "gasid3");
client.header("Content-Type", MediaType.APPLICATION_JSON_TYPE);
LOG.info("== Invoking REST Call for service ==" + url);
//till this
//i want this to be execute in thread
VehicleServiceProxy proxy = JAXRSClientFactory.create(url, VehicleServiceProxy.class, providers);
Client client = WebClient.client(proxy);
client.type(MediaType.APPLICATION_JSON);
//client.accept(MediaType.APPLICATION_JSON);
client.header("ST_USER", "gasid3");
client.header("Content-Type", MediaType.APPLICATION_JSON_TYPE);
LOG.info("== Invoking REST Call for service ==" + url);
//till this
/*Set<String> UniqueDmsId = new HashSet<>(dmsIdList);
dmsIdList.clear();
dmsIdList.addAll(UniqueDmsId);*/
return dmsIdList;
}
private String getServiceUrl(STouchServiceEnum stouchService)
{
String url="";
switch(stouchService)
{
case CUSTOMER:
//url="http://BLRKEC327951D:8080/stouch-admin-services/api";
url=customerUrl;
//url=customerUrl.concat("/User/dmsMap");
break;
case VEHICLE:
url=vehicleUrl.concat("/User/dmsMap");;
break;
default:
break;
}
return url;
}
}
在上面的代碼中,我想CustomerServiceProxy到在一個線程然後vehicleServiceProxy在另一個被執行,我想聚集的結果,一旦兩個線程執行得到完成。
有人可以幫助我嗎?
這似乎是您的解決方案:https://stackoverflow.com/questions/ 3489543 /如何調用java中的獨立線程的方法 –
嗨,在他們已經通過傳遞參數手動實現了runnable和創建對象的鏈接,但在我的情況下它是匿名線程和彈簧正在爲我的班級創建對象。我如何從我在我的類中聲明的多個線程向我的arrayList添加值? –