将实例化类型传递给泛型参数

时间:2016-11-28 21:08:08

标签: java

我正在尝试根据ConsumerTypes列表实例化一些RPCConsumers,如下所示。

问题是RPCConsumer显然不会得到类型T和R.当调用Function apply方法时,会得到一个强制转换异常。

我知道我需要将这些类型传递给RPCConsumer - 这是我需要使用反射的地方吗?我也可能不必要地过度复杂化。

TIA

public class ConsumerType<T, R> {

    private final RPCQueue queue;
    private final Function<T, R> actionFunction;

    public ConsumerType(RPCQueue queue, Function actionFunction) {
        this.queue = queue;
        this.actionFunction = actionFunction;
    }

    public RPCQueue getQueue() {
        return queue;
    }

    public Function getActionFunction() {
        return actionFunction;
    }

}




 public class RPCConsumer<T, R> extends DefaultConsumer implements Callable<Void> {

    private Channel channel;
    private String queueName;
    private Function<T,R> function;

    public RPCConsumer(Channel channel, String queueName, Function<T, R> function) throws IOException {
        // Constructor
    }

    // METHODS ETC

}

public class RPCConsumerFactory {

    private List<ConsumerType> consumerTypes;
    private ExecutorService executorService;

    public RPCConsumerFactory(List<ConsumerType> consumerTypes) throws NoHealthyServiceException {
        this.consumerTypes = consumerTypes;
        this.executorService = Executors.newFixedThreadPool(consumerTypes.size());
    }

    public void createRPCConsumers() throws IOException, TimeoutException {
        Connection connection = connectionFactory.createConnection();
        for(ConsumerType consumerType : consumerTypes) {
            Channel channel = connection.createChannel();

            String queueName = consumerType.getQueue().getName();
            Function actionFunction = consumerType.getActionFunction();

            executorService.submit(new RPCConsumer(channel, queueName, actionFunction));
        }

    }
}

1 个答案:

答案 0 :(得分:0)

第一个问题:原始类型

public ConsumerType(RPCQueue queue, Function actionFunction) {
    this.queue = queue;
    this.actionFunction = actionFunction;
}

参数类型应为Function< T, R >

public Function getActionFunction() {
    return actionFunction;
}

返回类型应为Function< T, R >。这些类型参数由类声明绑定。

private List<ConsumerType> consumerTypes;

我想你想要一个异类列表。那将是List< ConsumerType< ?, ? > >,这是你将会遇到困难的地方。

第二个问题:异构列表

通用类型仅在编译时存在,并且仅用于检查可以静态检查的内容。根据定义,ConsumerType任意类型的异构列表将不是静态类型可检查的。但是,以下内容应该有效:

public class RPCConsumer<T, R> extends DefaultConsumer implements Callable<Void> {

    private Channel channel;
    private String queueName;
    private Function<T,R> function;

    public static < T, R > RPCConsumer< T, R > make( Channel channel, String queueName, Function<T, R> function) throws IOException {
        return new RPCConsumer< T, R >( channel, queueName, function );
    }

    public RPCConsumer(Channel channel, String queueName, Function<T, R> function) throws IOException {
        // Constructor
    }

    // METHODS ETC

}
public class RPCConsumerFactory {

    private List<ConsumerType<?, ?>> consumerTypes;
    private ExecutorService executorService;

    public RPCConsumerFactory(List<ConsumerType<?, ?>> consumerTypes) throws NoHealthyServiceException {
        this.consumerTypes = consumerTypes;
        this.executorService = Executors.newFixedThreadPool(consumerTypes.size());
    }

    public void createRPCConsumers() throws IOException, TimeoutException {
        Connection connection = connectionFactory.createConnection();
        for(ConsumerType<?, ?> consumerType : consumerTypes) {
            Channel channel = connection.createChannel();

            String queueName = consumerType.getQueue().getName();
            Function<?, ?> actionFunction = consumerType.getActionFunction();

            executorService.submit(RPCConsumer.make(channel, queueName, actionFunction));
        }

    }
}