下面列出了怎么用com.hazelcast.core.IQueue的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void run() {
IQueue<String> demoQueue = hazelcastInstance.getQueue("demo.queue");
while (!shutdown) {
String data = null;
try {
data = demoQueue.poll(2, TimeUnit.SECONDS);
}
catch (InterruptedException ex) {
// ignore
}
if (data != null) {
log.info("Read data: {}", data);
}
}
}
public void awaitEmpty( IQueue<?> queue ) throws InterruptedException{
synchronized( monitor ){
if( !queue.isEmpty() ){
monitor.wait();
}
}
}
private static void consume(HazelcastInstance hazelcastInstance) {
IQueue<String> cola = hazelcastInstance.getQueue("cola");
while (true){
try {
System.out.println("Taken from queue: "+cola.take());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private static void produce(HazelcastInstance hazelcastInstance) {
IQueue<String> cola = hazelcastInstance.getQueue("cola");
int count=0;
while (true){
try {
cola.offer(Integer.toString(count++));
Thread.sleep(1000);
System.out.println("Added to queue. It has now "+cola.size());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public <E> IQueue<E> getQueue(String s) {
return null;
}
private IQueue<WorkUnit> getWorkQueue(){
return hcInstance.getQueue( WORK_QUEUE_NAME );
}
public IsEmptyListener( IQueue<WorkUnit> queue ){
this.queue = queue;
}
/**
* Creates an {@link IQueue} proxy around a {@link TransactionalQueue}. This
* allows for common handling of queues regardless of if they are
* transactional or not. Ideally Hazelcast's transactional queue would
* directly implement IQueue but that isn't the case.
*
* @param <E> the type of objects in the queue
* @param queue the transaction queue to create the proxy around
*
* @return the proxy to the transactional queue
*/
@SuppressWarnings("unchecked")
public static <E> IQueue<E> createQueueProxy(TransactionalQueue<E> queue) {
InvocationHandler handler = new TransactionalQueueInvocationHandler<>(
queue);
return (IQueue<E>) Proxy.newProxyInstance(
queue.getClass().getClassLoader(), new Class[]{IQueue.class},
handler);
}
/**
* Creates an {@link IQueue} proxy around a standard
* {@link ArrayBlockingQueue}. This allows for common handling of queues
* regardless of if they are standard Java queues or Hazelcast created queues.
*
* @param <E> the type of objects in the queue
* @param queue the blocking queue to create the proxy around
*
* @return the proxy to the blocking queue
*/
@SuppressWarnings("unchecked")
public static <E> IQueue<E> createQueueProxy(final ArrayBlockingQueue queue) {
InvocationHandler handler = new AbstractQueueInvocationHandler<>(queue);
return (IQueue<E>) Proxy.newProxyInstance(
IQueue.class.getClassLoader(), new Class[]{IQueue.class},
handler);
}