|
| 1 | +package net.authorize.sample.PaymentTransactions; |
| 2 | + |
| 3 | +import java.math.BigDecimal; |
| 4 | +import java.math.RoundingMode; |
| 5 | +import java.security.SecureRandom; |
| 6 | +import java.text.DecimalFormat; |
| 7 | +import java.util.Date; |
| 8 | +import java.util.concurrent.ExecutorService; |
| 9 | +import java.util.concurrent.Executors; |
| 10 | + |
| 11 | +import net.authorize.Environment; |
| 12 | +import net.authorize.api.contract.v1.*; |
| 13 | +import net.authorize.api.controller.CreateTransactionController; |
| 14 | +import net.authorize.api.controller.base.ApiOperationBase; |
| 15 | +import net.authorize.sample.CustomerProfiles.CreateCustomerPaymentProfile; |
| 16 | +import net.authorize.sample.CustomerProfiles.CreateCustomerProfile; |
| 17 | + |
| 18 | +public class ChargeCustomerProfilesMT { |
| 19 | + |
| 20 | + private static final int MYTHREADS = 120; |
| 21 | + private static String apiLoginId = "5KP3u95bQpv"; |
| 22 | + private static String transactionKey = "346HZ32z3fP4hTG2"; |
| 23 | + |
| 24 | + static SecureRandom rgenerator = new SecureRandom(); |
| 25 | + |
| 26 | + private static Double getAmount() |
| 27 | + { |
| 28 | + double d = (double)(1.05 + (450.0 * rgenerator.nextDouble())); |
| 29 | + DecimalFormat df = new DecimalFormat("#.##"); |
| 30 | + d = Double.valueOf(df.format(d)); |
| 31 | + return d; |
| 32 | + } |
| 33 | + |
| 34 | + private static String getEmail() |
| 35 | + { |
| 36 | + return rgenerator.nextInt(10000000) + "@test.com"; |
| 37 | + } |
| 38 | + |
| 39 | + // |
| 40 | + // Run this sample from command line with: |
| 41 | + // java -jar target/ChargeCreditCard-jar-with-dependencies.jar |
| 42 | + // |
| 43 | + public static Boolean run() { |
| 44 | + |
| 45 | + |
| 46 | + ExecutorService executor = Executors.newFixedThreadPool(MYTHREADS); |
| 47 | + |
| 48 | + Date dt1 = new Date(); |
| 49 | + for (int i = 0; i < MYTHREADS; i++) { |
| 50 | + Runnable worker = new MyRunnable(); |
| 51 | + executor.execute(worker); |
| 52 | + } |
| 53 | + executor.shutdown(); |
| 54 | + |
| 55 | + // Wait until all threads are finish |
| 56 | + while (!executor.isTerminated()) { |
| 57 | + |
| 58 | + } |
| 59 | + Date dt2 = new Date(); |
| 60 | + long difference = dt2.getTime() - dt1.getTime(); |
| 61 | + |
| 62 | + System.out.println("\nFinished all threads : " + difference/1000 + " sec"); |
| 63 | + |
| 64 | + return true; |
| 65 | + } |
| 66 | + |
| 67 | + public static class MyRunnable implements Runnable { |
| 68 | + |
| 69 | + @Override |
| 70 | + public void run() { |
| 71 | + CreateCustomerProfileResponse response = null; |
| 72 | + CreateCustomerPaymentProfileResponse paymentProfileResponse = null; |
| 73 | + |
| 74 | + try{ |
| 75 | + response = (CreateCustomerProfileResponse) CreateCustomerProfile.run(apiLoginId, transactionKey, getEmail()); |
| 76 | + paymentProfileResponse = (CreateCustomerPaymentProfileResponse) CreateCustomerPaymentProfile.run(apiLoginId, transactionKey, response.getCustomerProfileId()); |
| 77 | + } |
| 78 | + catch (Exception e) { |
| 79 | + System.out.println("Failure in CIM calls."); |
| 80 | + System.out.println(e.getMessage()); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + try { |
| 85 | + Date dt1 = new Date(); |
| 86 | + System.out.println("============================================================================"); |
| 87 | + System.out.println("chargeResponse Dt1: + " + dt1.toString()); |
| 88 | + |
| 89 | + if((response.getCustomerProfileId() != null) && (paymentProfileResponse.getCustomerPaymentProfileId() != null)) |
| 90 | + { |
| 91 | + CreateTransactionResponse chargeResponse = (CreateTransactionResponse) ChargeCustomerProfile.run(apiLoginId, transactionKey, |
| 92 | + response.getCustomerProfileId(), paymentProfileResponse.getCustomerPaymentProfileId(), getAmount()); |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + System.out.println("NULL PROFILE IDS."); |
| 97 | + } |
| 98 | + |
| 99 | + Date dt2 = new Date(); |
| 100 | + System.out.println("chargeResponse Dt2: + " + dt2.toString()); |
| 101 | + |
| 102 | + long difference = dt2.getTime() - dt1.getTime(); |
| 103 | + System.out.println("chargeResponse Diff: " + difference/1000); |
| 104 | + System.out.println("============================================================================"); |
| 105 | + } catch (Exception e) { |
| 106 | + System.out.println("Failure in charge customer profile call:"); |
| 107 | + System.out.println(e.getMessage()); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments