use of cbit.vcell.message.VCQueueConsumer in project vcell by virtualcell.
the class SimDataServer method init.
public void init() throws Exception {
String dataRequestFilter = "(" + VCMessagingConstants.MESSAGE_TYPE_PROPERTY + "='" + VCMessagingConstants.MESSAGE_TYPE_RPC_SERVICE_VALUE + "') " + " AND (" + VCMessagingConstants.SERVICE_TYPE_PROPERTY + "='" + ServiceType.DATA.getName() + "')";
String exportOnlyFilter = "(" + ServiceType.DATAEXPORT.getName() + " is NOT NULL)";
String dataOnlyFilter = "(" + ServiceType.DATAEXPORT.getName() + " is NULL)";
VCMessageSelector selector;
ServiceType serviceType = serviceInstanceStatus.getType();
int numThreads;
if (serviceType == ServiceType.DATAEXPORT) {
selector = vcMessagingService.createSelector(dataRequestFilter + " AND " + exportOnlyFilter);
numThreads = Integer.parseInt(PropertyLoader.getProperty(PropertyLoader.exportdataThreadsProperty, "3"));
} else if (serviceType == ServiceType.DATA) {
selector = vcMessagingService.createSelector(dataRequestFilter + " AND " + dataOnlyFilter);
numThreads = Integer.parseInt(PropertyLoader.getProperty(PropertyLoader.simdataThreadsProperty, "5"));
} else {
throw new RuntimeException("expecting either Service type of " + ServiceType.DATA + " or " + ServiceType.DATAEXPORT);
}
this.sharedProducerSession = vcMessagingService.createProducerSession();
rpcMessageHandler = new VCRpcMessageHandler(dataServerImpl, VCellQueue.DataRequestQueue);
this.pooledQueueConsumer = new VCPooledQueueConsumer(rpcMessageHandler, numThreads, sharedProducerSession);
this.pooledQueueConsumer.initThreadPool();
rpcConsumer = new VCQueueConsumer(VCellQueue.DataRequestQueue, pooledQueueConsumer, selector, serviceType.getName() + " RPC Server Thread", MessageConstants.PREFETCH_LIMIT_DATA_REQUEST);
vcMessagingService.addMessageConsumer(rpcConsumer);
initControlTopicListener();
}
use of cbit.vcell.message.VCQueueConsumer in project vcell by virtualcell.
the class TestRPC method main.
public static void main(String[] args) throws Exception {
try {
PropertyLoader.loadProperties();
VCMessagingService messagingService = VCellServiceHelper.getInstance().loadService(VCMessagingService.class);
messagingService.setDelegate(new SimpleMessagingDelegate());
// reading message and computing sum
// create N comsumers
MyRpcServer myRpcServer = new MyRpcServer();
VCRpcMessageHandler rpcMessageHandler = new VCRpcMessageHandler(myRpcServer, VCellTestQueue.JimQueue);
VCPooledQueueConsumer pooledQueueConsumer = new VCPooledQueueConsumer(rpcMessageHandler, NUM_THREADS, messagingService.createProducerSession());
pooledQueueConsumer.initThreadPool();
VCQueueConsumer rpcConsumer = new VCQueueConsumer(VCellTestQueue.JimQueue, pooledQueueConsumer, null, "Queue[" + VCellTestQueue.JimQueue.getName() + "] ==== RPC Consumer Master Thread ", 1000);
// VCRpcMessageHandler rpcMessageHandler = new VCRpcMessageHandler(myRpcServer, VCellQueue.JimQueue, log);
// VCQueueConsumer rpcConsumer = new VCQueueConsumer(VCellQueue.JimQueue, rpcMessageHandler, null, "Queue["+VCellQueue.JimQueue.getName()+"] ==== RPC Consumer Master Thread ", 1000);
messagingService.addMessageConsumer(rpcConsumer);
for (int i = 0; i < NUM_MESSAGES; i++) {
// creating one messageProducer session
final VCMessageSession messageSession = messagingService.createProducerSession();
class MyTask implements Runnable {
int msgNum;
MyTask(int msgNum) {
this.msgNum = msgNum;
}
public void run() {
try {
//
// create simple RPC request for service "Testing_Service"
//
User user = new User("schaff", new KeyValue("17"));
Integer n1 = new Integer(msgNum);
Integer n2 = new Integer(msgNum + 1);
VCRpcRequest rpcRequest = new VCRpcRequest(user, RpcServiceType.TESTING_SERVICE, "add", new Object[] { n1, n2 });
//
// send request and block for response (or timeout).
// RPC invocations don't need commits.
//
Object returnValue = messageSession.sendRpcMessage(VCellTestQueue.JimQueue, rpcRequest, true, 20000, null, null, null);
//
if (returnValue instanceof Integer) {
System.out.println("add(" + n1 + "," + n2 + ") ===> " + returnValue);
} else {
System.out.println("unexpected return value of " + returnValue);
}
} catch (VCMessagingInvocationTargetException e) {
e.printStackTrace(System.out);
System.out.println("the rpc service threw an exception");
e.getTargetException().printStackTrace(System.out);
} catch (VCMessagingException e) {
e.printStackTrace();
}
}
}
;
new Thread(new MyTask(i)).start();
}
Thread.sleep(2000);
System.out.println("main program calling closeAll()");
pooledQueueConsumer.shutdownAndAwaitTermination();
messagingService.close();
System.out.println("main program exiting");
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
use of cbit.vcell.message.VCQueueConsumer in project vcell by virtualcell.
the class TestBlobMessages method testBlobMessages.
@Test
public void testBlobMessages() throws VCMessagingException {
// creating one messageProducer session
ArrayList<VCMessageSession> sessions = new ArrayList<VCMessageSession>();
for (int i = 0; i < NUM_PRODUCERS; i++) {
sessions.add(messagingService.createProducerSession());
}
AtomicInteger messagesToRead = new AtomicInteger(0);
long startTime_MS = System.currentTimeMillis();
final long TIMEOUT_MS = 1000 * 2;
for (int i = 0; i < NUM_MESSAGES; i++) {
for (int s = 0; s < NUM_PRODUCERS; s++) {
VCMessageSession session = sessions.get(s);
byte[] objectContent = new byte[40000 * (i + 1)];
Arrays.fill(objectContent, (byte) 22);
VCMessage message = session.createObjectMessage(objectContent);
// VCMessage message = session.createObjectMessage(new byte[100000000]);
session.sendQueueMessage(VCellTestQueue.JimQueue, message, false, 100000L);
session.commit();
int numMessages = messagesToRead.incrementAndGet();
System.out.println("sent message: messages unread = " + numMessages);
}
}
// create N comsumers
for (int i = 0; i < NUM_COMSUMERS; i++) {
QueueListener listener = new QueueListener() {
public void onQueueMessage(VCMessage vcMessage, VCMessageSession session) throws RollbackException {
Object objectContent = vcMessage.getObjectContent();
System.out.println("object content is '" + objectContent + "' of type " + objectContent.getClass().getName());
String blobInfo = "<no blob>";
if (vcMessage.propertyExists(VCMessageJms.BLOB_MESSAGE_MONGODB_OBJECTID)) {
String objectType = vcMessage.getStringProperty(VCMessageJms.BLOB_MESSAGE_OBJECT_TYPE);
int objectSize = vcMessage.getIntProperty(VCMessageJms.BLOB_MESSAGE_OBJECT_SIZE);
String objectid = vcMessage.getStringProperty(VCMessageJms.BLOB_MESSAGE_MONGODB_OBJECTID);
String persistenceType = vcMessage.getStringProperty(VCMessageJms.BLOB_MESSAGE_PERSISTENCE_TYPE);
blobInfo = "BLOB: persistenceType=" + persistenceType + ", object=" + objectType + ", size=" + objectSize + ", objectId=" + objectid;
}
System.out.println("<<<< onQueueMessage() >>>> timestampMS=" + vcMessage.getTimestampMS() + ", " + toString() + ", elapsedTimeS=" + ((System.currentTimeMillis() - vcMessage.getTimestampMS()) / 1000.0) + ", Received: " + blobInfo);
session.commit();
int numMessages = messagesToRead.decrementAndGet();
System.out.println("read message: messages unread = " + numMessages);
}
};
VCQueueConsumer queueConsumer = new VCQueueConsumer(VCellTestQueue.JimQueue, listener, null, "Queue[" + VCellTestQueue.JimQueue.getName() + "] ==== Consumer Thread " + i, 1);
messagingService.addMessageConsumer(queueConsumer);
}
System.out.println("waiting for messages to be read");
int numUnreadMessages = messagesToRead.get();
while (numUnreadMessages > 0) {
long elapsedTime = System.currentTimeMillis() - startTime_MS;
System.out.println("elapsed time is " + elapsedTime + " ms, " + numUnreadMessages + " unread messages");
if (elapsedTime > TIMEOUT_MS) {
throw new RuntimeException("timeout before processing all messages, " + numUnreadMessages + " unread");
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
numUnreadMessages = messagesToRead.get();
}
System.out.println("done");
}
use of cbit.vcell.message.VCQueueConsumer in project vcell by virtualcell.
the class TestQueues method main.
public static void main(String[] args) throws Exception {
try {
PropertyLoader.loadProperties();
VCMessagingService messagingService = new VCMessagingServiceEmbedded();
messagingService.setDelegate(new SimpleMessagingDelegate());
final Calculator calculator = new Calculator();
// creating one messageProducer session
ArrayList<VCMessageSession> sessions = new ArrayList<VCMessageSession>();
for (int i = 0; i < NUM_PRODUCERS; i++) {
sessions.add(messagingService.createProducerSession());
}
int sum = 0;
for (int i = 0; i < NUM_MESSAGES; i++) {
for (int s = 0; s < NUM_PRODUCERS; s++) {
sum++;
VCMessageSession session = sessions.get(s);
VCMessage message = session.createTextMessage("message " + i + " from session " + s);
session.sendQueueMessage(VCellTestQueue.JimQueue, message, false, 100000L);
session.commit();
}
Thread.sleep(2000);
}
System.out.println("Correct sum is " + sum);
Thread.sleep(30);
// create N comsumers
for (int i = 0; i < NUM_COMSUMERS; i++) {
QueueListener listener = new QueueListener() {
public void onQueueMessage(VCMessage vcMessage, VCMessageSession session) throws RollbackException {
// new Thread()
System.out.println("timestampMS=" + vcMessage.getTimestampMS() + ", " + toString() + ", elapsedTimeS=" + ((System.currentTimeMillis() - vcMessage.getTimestampMS()) / 1000.0) + ", Received: " + vcMessage.getTextContent());
// int number = Integer.parseInt(vcMessage.getTextContent());
calculator.add(1);
}
};
VCQueueConsumer queueConsumer = new VCQueueConsumer(VCellTestQueue.JimQueue, listener, null, "Queue[" + VCellTestQueue.JimQueue.getName() + "] ==== Consumer Thread " + i, 1);
messagingService.addMessageConsumer(queueConsumer);
}
while (calculator.getSum() < sum) {
System.out.println("calculator sum = " + calculator.getSum());
Thread.sleep(1000);
}
System.out.println("calculator sum = " + calculator.getSum());
System.out.println("main program calling closeAll()");
messagingService.close();
System.out.println("main program exiting");
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
Aggregations