繁体   English   中英

将实例化类型传递给通用参数

[英]Passing instantiated type into generic parameter

我试图根据如下的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));
        }

    }
}

第一个问题:原始类型

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< ?, ? > > 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));
        }

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM