Created
June 25, 2012 16:06
-
-
Save mmuruev/2989493 to your computer and use it in GitHub Desktop.
Client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package playtech.client; | |
import java.util.Random; | |
public class RandomUserGenerator { | |
private static final String[] userNames = {"john","jane"}; | |
private static final Random generator = new Random(); | |
private static final int RANGE = 100; | |
public String getUserName() | |
{ | |
return userNames[generator.nextInt(userNames.length)]; | |
} | |
public String getBalanceChange() | |
{ | |
return String.valueOf(generator.nextInt(RANGE) - generator.nextInt(RANGE)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package playtech.client; | |
import com.sun.jersey.api.client.Client; | |
import com.sun.jersey.api.client.ClientResponse; | |
import com.sun.jersey.api.client.WebResource; | |
import com.sun.jersey.api.client.config.ClientConfig; | |
import com.sun.jersey.api.client.config.DefaultClientConfig; | |
import playtech.service.WalletStatus; | |
import javax.ws.rs.core.MediaType; | |
import javax.ws.rs.core.UriBuilder; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.URI; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
public class Start { | |
private static final String BASE_URL = "http://localhost:8080/wallet/service"; | |
private static final String METHOD_NAME = "balance"; | |
public static void main(String[] args) throws IOException { | |
System.out.println("Press Enter for finish"); | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
while(!br.ready()) | |
{ | |
int threadsCount = 10; | |
if(args.length > 1) | |
{ | |
threadsCount = Integer.parseInt(args[0]); | |
} | |
List<Thread> threads = new ArrayList<Thread>(); | |
for (int i = 0; i < threadsCount; i++) { | |
Runnable task = new WebClient(BASE_URL, METHOD_NAME); | |
Thread worker = new Thread(task); | |
worker.setName(String.valueOf(i)); | |
worker.start(); | |
threads.add(worker); | |
} | |
boolean perform= false; | |
do{ | |
for (Thread thread : threads) { | |
perform = thread.isAlive(); | |
} | |
} while(perform); | |
System.out.println("Done. Press Enter for finish"); | |
try { | |
Thread.sleep(5000); | |
} catch (InterruptedException ie) { | |
System.out.println(ie.getMessage()); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package playtech.client; | |
import com.sun.jersey.api.client.ClientResponse; | |
import com.sun.jersey.api.client.WebResource; | |
import com.sun.jersey.api.client.config.ClientConfig; | |
import com.sun.jersey.api.client.config.DefaultClientConfig; | |
import playtech.service.WalletStatus; | |
import javax.ws.rs.core.MediaType; | |
import javax.ws.rs.core.UriBuilder; | |
import java.net.URI; | |
public class WebClient implements Runnable { | |
private static int transaction = 0; | |
private String baseURL = ""; | |
private String methodName = ""; | |
public WebClient(String baseURL, String method) | |
{ | |
this.baseURL = baseURL; | |
this.methodName = method; | |
} | |
@Override | |
public void run() { | |
ClientConfig clientConfig = new DefaultClientConfig(); | |
//clientConfig.getClasses().add(com.sun.jersey.impl.provider.entity.JSONRootElementProvider.class); | |
com.sun.jersey.api.client.Client client = com.sun.jersey.api.client.Client.create(clientConfig); | |
try { | |
Thread.sleep((int)Math.random() * 1000); | |
} catch (InterruptedException ie) { | |
System.out.println(ie.getMessage()); | |
} | |
WebResource resource = client.resource(getBaseURI()); | |
RandomUserGenerator user = new RandomUserGenerator(); | |
ClientResponse response = resource.path(methodName). | |
path(user.getUserName()). | |
path(getTransactionId()). | |
path(user.getBalanceChange()). | |
accept(MediaType.APPLICATION_JSON) | |
.post(ClientResponse.class); | |
if (response.getStatus() != 200) { | |
throw new RuntimeException("Failed : HTTP error code : " | |
+ response.getStatus()); | |
} | |
WalletStatus output = response.getEntity(WalletStatus.class); | |
System.out.println("Output from Server .... \n"); | |
if (output.errorCode == 0) { | |
System.out.println("Transaction: " + output.transactionId); | |
System.out.println("Balance: " + output.balanceAfterChange); | |
} else { | |
System.out.println("Error " + output.errorCode); | |
} | |
} | |
private URI getBaseURI() { | |
return UriBuilder.fromUri(baseURL).build(); | |
} | |
private String getTransactionId() { | |
return String.valueOf(transaction++); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment