gu.simplemq.Producer Maven / Gradle / Ivy
The newest version!
package gu.simplemq;
import java.lang.reflect.Type;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
/**
* 基于阻塞队列 {@link BlockingQueue} 实现生产者模型
* @author guyadong
*
* @param
*/
public class Producer extends IProducerSingle.AbstractHandler implements IQueueComponent{
protected BlockingQueue queue;
public static final ProducermakeInstance(Type type,BlockingQueue queue){
return new Producer(type, queue){};
}
protected Producer(Classclazz) {
super(clazz);
}
protected Producer(Type type) {
super(type);
}
protected Producer(Class clazz, BlockingQueue queue) {
this((Type)clazz,queue);
}
protected Producer(Type type, BlockingQueue queue) {
super(type);
this.queue = queue;
}
@Override
public boolean produce(T t,boolean offerLast){
if(! offerLast ){
if(queue instanceof BlockingDeque){
return ((BlockingDeque)queue).offerFirst(t);
}else{
throw new UnsupportedOperationException(" queue must be instance of BlockingDeque");
}
}else{
return queue.offer(t);
}
}
@Override
public boolean produce(T t){
if(this.offerLast){
return queue.offer(t);
}else{
return produce(t,offerLast);
}
}
@Override
public BlockingQueue getQueue() {
return queue;
}
public Producer setQueue(BlockingQueue queue) {
this.queue = queue;
return this;
}
@Override
public String getQueueName() {
return "unknow";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy