org.siddhi.sample.benchmark.client.StockQuoteClient Maven / Gradle / Ivy
The newest version!
/**
* 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 org.siddhi.sample.benchmark.client;
import org.siddhi.core.event.Event;
import org.siddhi.sample.benchmark.SimpleStockQuoteEventGenerator;
import org.siddhi.sample.benchmark.Symbols;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
public class StockQuoteClient extends Thread {
private Client client;
private SimpleStockQuoteEventGenerator simpleStockQuoteEventGenerator;
public StockQuoteClient(Client client) {
this.client = client;
simpleStockQuoteEventGenerator = new SimpleStockQuoteEventGenerator("CSEStream");
}
public void run() {
try {
begin();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void begin() throws IOException, InterruptedException {
SocketChannel socketChannel = null;
try {
socketChannel = SocketChannel.open(new InetSocketAddress(client.getHost(), client.getPort())); //open the socket channel for the given host on the given port.
} catch (IOException e) {
throw new RuntimeException(e);
}
ObjectOutputStream oos = new ObjectOutputStream(socketChannel.socket().getOutputStream());
int eventBatchSize = Symbols.SYMBOLS.length;
while (true) {
long ms = System.currentTimeMillis();
for (int i = 0; i < eventBatchSize; i++) { //generated events of the given batch size
Event event = simpleStockQuoteEventGenerator.generateEvent(i, 10, 10);
System.out.println("Sending Event........."+event);
oos.reset();
oos.writeObject(event);
}
if(client.getBatchWait() != 0) { // wait for the given batch wait time after completing one batch of events
Thread.sleep(client.getBatchWait());
}
}
}
}