Java tutorial
/* * Copyright 2012-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.everis.storage.service; import com.everis.storage.model.Transaction; import com.everis.storage.repository.TransactionRepository; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.netflix.appinfo.InstanceInfo; import com.netflix.discovery.DiscoveryClient; import com.netflix.discovery.shared.Application; import com.netflix.discovery.shared.Applications; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.message.BasicHeader; import org.apache.http.util.EntityUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "msstorage") public class StorageService { @Autowired private TransactionRepository repository; @Autowired private DiscoveryClient discoveryClient; //private EurekaClient eurekaClient; public boolean storeInMongoDB(Transaction transaction) { System.out.println("Storing in MongoDB..."); repository.insert(transaction); return true; } public boolean sendTransaction(Transaction transaction) { System.out.println("Sending transaction to Processing microservice..."); restClient(transaction); return true; } private InstanceInfo discoverService() { Applications applications = discoveryClient.getApplications(); InstanceInfo instance = null; for (Application app : applications.getRegisteredApplications()) { if ("PROCESSING".equals(app.getName())) { instance = app.getInstances().get(0); break; } } //InstanceInfo instance = discoveryClient.getNextServerFromEureka("PROCESSING", false); return instance; } private void restClient(Transaction transaction) { HttpClient httpClient = HttpClientBuilder.create().build(); try { InstanceInfo service = discoverService(); System.out.println("DISCOVERY: " + service); // specify the host, protocol, and port HttpHost target = new HttpHost(service.getHostName(), service.getPort(), "http"); // specify the get request HttpPost postRequest = new HttpPost("/transactions"); Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").create(); StringEntity jsonEntity = new StringEntity(gson.toJson(transaction)); postRequest.setEntity(jsonEntity); postRequest.addHeader(new BasicHeader("Content-type", "application/json")); System.out.println("executing request to " + target); HttpResponse httpResponse = httpClient.execute(target, postRequest); HttpEntity entity = httpResponse.getEntity(); System.out.println("----------------------------------------"); System.out.println(httpResponse.getStatusLine()); Header[] headers = httpResponse.getAllHeaders(); for (int i = 0; i < headers.length; i++) { System.out.println(headers[i]); } System.out.println("----------------------------------------"); if (entity != null) { System.out.println(EntityUtils.toString(entity)); } } catch (Exception e) { e.printStackTrace(); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpClient.getConnectionManager().shutdown(); } } }