Java tutorial
package com.audaexplore.b2b; import java.io.BufferedReader; import java.io.File; import java.io.FileDescriptor; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.net.UnknownHostException; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.TimeoutException; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import org.apache.commons.lang3.StringUtils; import com.google.gson.Gson; import com.jayway.jsonpath.JsonPath; import com.rabbitmq.client.AMQP; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import net.minidev.json.JSONArray; @Path("addfnol") public class GenAddFnol { public GenAddFnol() { } @GET @Path("test") @Produces(MediaType.TEXT_PLAIN) public String test() { return "tested"; } @POST @Produces(MediaType.TEXT_PLAIN) public String testPost() { return "testPost"; } @GET public boolean addFnol(@QueryParam("fnolsToGen") int fnolsToGen) { GenFnol genFnol = new GenFnol(); List<Fnol> fnolList = new ArrayList<Fnol>(); for (int i = 0; i < fnolsToGen; i++) { Fnol fnol = genFnol.genFnolData(); fnolList.add(fnol); } publishFnol(fnolList); return true; } public boolean addCars(@QueryParam("carsToCreate") int carsToCreate) { publishCars(createCars(carsToCreate)); return true; } private List<Car> createCars(int carsToCreate) { Car car; List<Car> carList = new ArrayList(); for (int i = 0; i < carsToCreate; i++) { car = new Car(); car.setBodyStyle(car.getBodyStyle()); car.setColor(car.getColor()); car.setMake(car.getMake()); car.setIsFast(car.getIsFast()); carList.add(car); } return carList; } private void publishFnol(List<Fnol> fnolList) { String amqpURI = null; ConnectionFactory factory = null; Channel channel = null; Connection connection = null; String message; String BOUND_SERVICES_ENV_VARIABLE_NAME = "VCAP_SERVICES"; Map<String, String> env = System.getenv(); String boundServicesJson = env.get(BOUND_SERVICES_ENV_VARIABLE_NAME); //String boundServicesJson="{\"staging_env_json\":{},\"running_env_json\":{},\"system_env_json\":{\"VCAP_SERVICES\":{\"cloudamqp\":[{\"name\":\"MQRabbit\",\"label\":\"cloudamqp\",\"tags\":[\"Web-based\",\"UserProvisioning\",\"MessagingandQueuing\",\"amqp\",\"Backup\",\"SingleSign-On\",\"NewProduct\",\"rabbitmq\",\"CertifiedApplications\",\"Android\",\"DeveloperTools\",\"DevelopmentandTestTools\",\"Buyable\",\"Messaging\",\"Importable\",\"ITManagement\"],\"plan\":\"lemur\",\"credentials\":{\"uri\":\"amqp://kujcbqju:xr-HKmBcq5Lv87CCrYlQ6NaVmunhU8cv@moose.rmq.cloudamqp.com/kujcbqju\",\"http_api_uri\":\"https://kujcbqju:xr-HKmBcq5Lv87CCrYlQ6NaVmunhU8cv@moose.rmq.cloudamqp.com/api/\"}}],\"newrelic\":[{\"name\":\"NewRelic\",\"label\":\"newrelic\",\"tags\":[\"Monitoring\"],\"plan\":\"standard\",\"credentials\":{\"licenseKey\":\"a8a96a124d1b58d708a2c4c07c6cff8938e2e2f4\"}}],\"mongolab\":[{\"name\":\"MongoDB\",\"label\":\"mongolab\",\"tags\":[\"DataStore\",\"document\",\"mongodb\"],\"plan\":\"sandbox\",\"credentials\":{\"uri\":\"mongodb://CloudFoundry_31lvrquo_j44bi0vu_3gjp7i4s:6RAtFVBfQUCe_DV7LAq5uCffOXaEXdly@ds047315.mongolab.com:47315/CloudFoundry_31lvrquo_j44bi0vu\"}}]}},\"application_env_json\":{\"VCAP_APPLICATION\":{\"limits\":{\"mem\":512,\"disk\":1024,\"fds\":16384},\"application_id\":\"87bdc475-83c4-4df9-92d1-40ff9bf82249\",\"application_version\":\"52891578-5906-4846-9231-afe7048f29bf\",\"application_name\":\"vinservice\",\"application_uris\":[\"vinservice.cfapps.io\"],\"version\":\"52891578-5906-4846-9231-afe7048f29bf\",\"name\":\"vinservice\",\"space_name\":\"development\",\"space_id\":\"d33d438c-860a-46d3-ab33-4c2efac841be\",\"uris\":[\"vinservice.cfapps.io\"],\"users\":null}}}"; if (StringUtils.isNotBlank(boundServicesJson)) { //amqpURI = JsonPath.read(boundServicesJson, "$..cloudamqp[0].credentials.uri",String.class); JSONArray jsonArray = JsonPath.read(boundServicesJson, "$..cloudamqp[0].credentials.uri"); amqpURI = jsonArray.get(0).toString(); } else { amqpURI = "amqp://localhost"; } System.out.println("Sending messages to " + amqpURI); //System.exit(0); try { factory = new ConnectionFactory(); factory.setUri(amqpURI); connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare("fnol", true, false, false, null); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (TimeoutException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (KeyManagementException e1) { e1.printStackTrace(); } catch (NoSuchAlgorithmException e1) { e1.printStackTrace(); } catch (URISyntaxException e1) { e1.printStackTrace(); } for (Fnol fnol : fnolList) { message = new Gson().toJson(fnol); try { channel.basicPublish("amq.direct", "fnolKey", new AMQP.BasicProperties.Builder().contentType("text/plain").deliveryMode(2).build(), //MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes()); System.out.println("message " + message + " was published"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } try { channel.close(); connection.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TimeoutException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void publishCars(List<Car> carList) { String amqpURI = null; ConnectionFactory factory = null; Channel channel = null; Connection connection = null; String message; String BOUND_SERVICES_ENV_VARIABLE_NAME = "VCAP_SERVICES"; Map<String, String> env = System.getenv(); String boundServicesJson = env.get(BOUND_SERVICES_ENV_VARIABLE_NAME); //String boundServicesJson="{\"staging_env_json\":{},\"running_env_json\":{},\"system_env_json\":{\"VCAP_SERVICES\":{\"cloudamqp\":[{\"name\":\"MQRabbit\",\"label\":\"cloudamqp\",\"tags\":[\"Web-based\",\"UserProvisioning\",\"MessagingandQueuing\",\"amqp\",\"Backup\",\"SingleSign-On\",\"NewProduct\",\"rabbitmq\",\"CertifiedApplications\",\"Android\",\"DeveloperTools\",\"DevelopmentandTestTools\",\"Buyable\",\"Messaging\",\"Importable\",\"ITManagement\"],\"plan\":\"lemur\",\"credentials\":{\"uri\":\"amqp://kujcbqju:xr-HKmBcq5Lv87CCrYlQ6NaVmunhU8cv@moose.rmq.cloudamqp.com/kujcbqju\",\"http_api_uri\":\"https://kujcbqju:xr-HKmBcq5Lv87CCrYlQ6NaVmunhU8cv@moose.rmq.cloudamqp.com/api/\"}}],\"newrelic\":[{\"name\":\"NewRelic\",\"label\":\"newrelic\",\"tags\":[\"Monitoring\"],\"plan\":\"standard\",\"credentials\":{\"licenseKey\":\"a8a96a124d1b58d708a2c4c07c6cff8938e2e2f4\"}}],\"mongolab\":[{\"name\":\"MongoDB\",\"label\":\"mongolab\",\"tags\":[\"DataStore\",\"document\",\"mongodb\"],\"plan\":\"sandbox\",\"credentials\":{\"uri\":\"mongodb://CloudFoundry_31lvrquo_j44bi0vu_3gjp7i4s:6RAtFVBfQUCe_DV7LAq5uCffOXaEXdly@ds047315.mongolab.com:47315/CloudFoundry_31lvrquo_j44bi0vu\"}}]}},\"application_env_json\":{\"VCAP_APPLICATION\":{\"limits\":{\"mem\":512,\"disk\":1024,\"fds\":16384},\"application_id\":\"87bdc475-83c4-4df9-92d1-40ff9bf82249\",\"application_version\":\"52891578-5906-4846-9231-afe7048f29bf\",\"application_name\":\"vinservice\",\"application_uris\":[\"vinservice.cfapps.io\"],\"version\":\"52891578-5906-4846-9231-afe7048f29bf\",\"name\":\"vinservice\",\"space_name\":\"development\",\"space_id\":\"d33d438c-860a-46d3-ab33-4c2efac841be\",\"uris\":[\"vinservice.cfapps.io\"],\"users\":null}}}"; if (StringUtils.isNotBlank(boundServicesJson)) { //amqpURI = JsonPath.read(boundServicesJson, "$..cloudamqp[0].credentials.uri",String.class); JSONArray jsonArray = JsonPath.read(boundServicesJson, "$..cloudamqp[0].credentials.uri"); amqpURI = jsonArray.get(0).toString(); } else { amqpURI = "amqp://localhost"; } System.out.println("Sending messages to " + amqpURI); //System.exit(0); try { factory = new ConnectionFactory(); factory.setUri(amqpURI); connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare("fnol", true, false, false, null); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (TimeoutException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (KeyManagementException e1) { e1.printStackTrace(); } catch (NoSuchAlgorithmException e1) { e1.printStackTrace(); } catch (URISyntaxException e1) { e1.printStackTrace(); } for (Car car : carList) { message = new Gson().toJson(car); try { channel.basicPublish("amq.direct", "fnolKey", new AMQP.BasicProperties.Builder().contentType("text/plain").deliveryMode(2).build(), //MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes()); System.out.println("message " + message + " was published"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } try { channel.close(); connection.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TimeoutException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { GenAddFnol addFnol = new GenAddFnol(); addFnol.addFnol(5); //String temp = addFnol.test(); //System.out.println(temp); } class Car { String color; String bodyStyle; String make; String isFast; public void setColor(String color) { this.color = color; } public void setBodyStyle(String bodyStyle) { this.bodyStyle = bodyStyle; } public void setMake(String make) { this.make = make; } public void setIsFast(String isFast) { this.isFast = isFast; } private String getBodyStyle() { boolean flag = true; double random = 0; String bodyStyle = null; while (flag) { random = Math.random() * 100; random = Math.floor(random); if (random > 0 && random < 5) flag = false; } switch (new Double(random).intValue()) { case 1: bodyStyle = "4dr"; break; case 2: bodyStyle = "2dr"; break; case 3: bodyStyle = "convertible"; break; case 4: bodyStyle = "coupe"; break; } // System.out.println(bodyStyle); return bodyStyle; } private String getColor() { boolean flag = true; double random = 0; String color = null; while (flag) { random = Math.random() * 100; random = Math.floor(random); if (random > 0 && random < 5) flag = false; } switch (new Double(random).intValue()) { case 1: color = "red"; break; case 2: color = "blue"; break; case 3: color = "white"; break; case 4: color = "black"; break; } // System.out.println(bodyStyle); return color; } private String getMake() { boolean flag = true; double random = 0; String make = null; while (flag) { random = Math.random() * 100; random = Math.floor(random); if (random > 0 && random < 5) flag = false; } switch (new Double(random).intValue()) { case 1: make = "ford"; break; case 2: make = "toyota"; break; case 3: make = "jeep"; break; case 4: make = "porsche"; break; } // System.out.println(bodyStyle); return make; } private String getIsFast() { boolean flag = true; double random = 0; String isFast = null; while (flag) { random = Math.random() * 100; random = Math.floor(random); if (random > 0 && random < 3) flag = false; } switch (new Double(random).intValue()) { case 1: isFast = "true"; break; case 2: isFast = "false"; break; } // System.out.println(isFast); return isFast; } } }