Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package inquirystrestests;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.text.SimpleDateFormat;
- import java.util.HashMap;
- import java.util.Map;
- import org.joda.time.DateTime;
- import org.joda.time.Seconds;
- /**
- *
- * @author anggi
- */
- public class InquiryStresTests {
- private final static String IP_SERVER = "103.16.138.19";
- private final static Integer PORT_SERVER = 9999;
- private final static SimpleDateFormat formatDateTime = new SimpleDateFormat("yyyyMMddhhmmss");
- private final static String fileResultName = "/home/anggi/Desktop/result_tests.csv";
- public static void main(String[] args) throws IOException {
- createHeader();
- String csvFile = "/home/anggi/Desktop/Id_Pelanggan.csv";
- BufferedReader br = null;
- String line = "";
- String cvsSplitBy = ",";
- try {
- br = new BufferedReader(new FileReader(csvFile));
- while ((line = br.readLine()) != null) {
- String[] idpel = line.split(cvsSplitBy);
- asyncServiceMethod(idpel[0]);
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (br != null) {
- try {
- br.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- public static void createHeader() throws IOException {
- FileWriter writer = new FileWriter(fileResultName);
- writer.append("idpel ");
- writer.append('|');
- writer.append("stan ");
- writer.append('|');
- writer.append("waktu_request ");
- writer.append('|');
- writer.append("waktu_response");
- writer.append('|');
- writer.append("selisih ");
- writer.append('|');
- writer.append("response_code");
- writer.append('\n');
- writer.flush();
- writer.close();
- }
- public static void writeResult(Map<String, String> data) {
- try {
- FileWriter writer = new FileWriter(fileResultName, true);
- writer.append(data.get("idPel"));
- writer.append('|');
- writer.append(data.get("stan"));
- writer.append('|');
- writer.append(data.get("waktu_request"));
- writer.append('|');
- writer.append(data.get("waktu_response"));
- writer.append('|');
- writer.append(data.get("selisih"));
- writer.append('|');
- writer.append(data.get("response_code"));
- writer.append('\n');
- //generate whatever data you want
- writer.flush();
- writer.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void sendMessage(String IdPel) throws IOException {
- DateTime requestTime = new DateTime();
- Socket clientSocket = new Socket(IP_SERVER, PORT_SERVER);
- clientSocket.setSoTimeout(10 * 1000);
- String stan = "000569536414";
- Map<String, String> message = new HashMap<>();
- StringBuilder messageRequest = new StringBuilder();
- messageRequest.append("2100"); //MTI
- messageRequest.append("4030004180810000");//BITMAP
- messageRequest.append("05"); // length of PAN
- messageRequest.append("99501"); // PAN
- messageRequest.append(stan); //STAN
- messageRequest.append(formatDateTime.format(requestTime.toDate())); //TRANSACTION DATETIME
- messageRequest.append("6021"); //MERCHANT CODE
- messageRequest.append("07"); //Length of Bank Code
- messageRequest.append("4510017"); //Bank Code
- messageRequest.append("07"); //Length of CID
- messageRequest.append("4511014"); //CID
- messageRequest.append("L123 "); //Terminal ID
- messageRequest.append("019"); //length of additional Private Data
- //additional Private Data (bit 48)
- // =======================================================================
- messageRequest.append("0000000"); // Switcher id
- messageRequest.append(IdPel); // ID PEL
- PrintWriter outgoing = new PrintWriter(clientSocket.getOutputStream());
- InputStreamReader incoming = new InputStreamReader(clientSocket.getInputStream());
- String trailer = new String(new char[]{10});
- //kirim
- System.out.println("send message");
- outgoing.print(messageRequest.toString() + trailer);
- outgoing.flush();
- int data;
- StringBuffer sb = new StringBuffer();
- while ((data = incoming.read()) != 10) {
- sb.append((char) data);
- }
- System.out.println("Rec. Msg [" + sb.toString() + "] len [" + sb.toString().length() + "]");
- String[] xx = sb.toString().split("4511014");
- String RC = xx[1].substring(0, 4);
- DateTime responseTIme = new DateTime();
- String selisih = Seconds.secondsBetween(requestTime.withMillisOfSecond(0), responseTIme.withMillisOfSecond(0)).getSeconds()+ " seconds";
- message.put("idPel", IdPel);
- message.put("stan", stan);
- message.put("waktu_request", formatDateTime.format(requestTime.toDate()));
- message.put("waktu_response", formatDateTime.format(responseTIme.toDate()));
- message.put("selisih", selisih);
- message.put("response_code", RC);
- writeResult(message);
- outgoing.close();
- incoming.close();
- clientSocket.close();
- }
- public static void asyncServiceMethod(final String idPel) {
- Runnable task = new Runnable() {
- @Override
- public void run() {
- try {
- sendMessage(idPel);
- } catch (Exception ex) {
- //handle error which cannot be thrown back
- }
- }
- };
- new Thread(task, "ServiceThread").start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement