@Test public void testCustomPartitionCountOverridesPartitioningIfLarger() throws Exception { byte[] ratherBigPayload = new byte[2048]; Arrays.fill(ratherBigPayload, (byte) 65); KafkaTestBinder binder = (KafkaTestBinder) getBinder(); DirectChannel moduleOutputChannel = new DirectChannel(); QueueChannel moduleInputChannel = new QueueChannel(); Properties producerProperties = new Properties(); producerProperties.put(BinderProperties.MIN_PARTITION_COUNT, "5"); producerProperties.put(BinderProperties.NEXT_MODULE_COUNT, "3"); producerProperties.put(BinderProperties.PARTITION_KEY_EXPRESSION, "payload"); Properties consumerProperties = new Properties(); consumerProperties.put(BinderProperties.MIN_PARTITION_COUNT, "5"); long uniqueBindingId = System.currentTimeMillis(); binder.bindProducer("foo" + uniqueBindingId + ".0", moduleOutputChannel, producerProperties); binder.bindConsumer("foo" + uniqueBindingId + ".0", moduleInputChannel, consumerProperties); Message<?> message = org.springframework.integration.support.MessageBuilder.withPayload(ratherBigPayload) .build(); // Let the consumer actually bind to the producer before sending a msg binderBindUnbindLatency(); moduleOutputChannel.send(message); Message<?> inbound = moduleInputChannel.receive(2000); assertNotNull(inbound); assertArrayEquals(ratherBigPayload, (byte[]) inbound.getPayload()); Collection<Partition> partitions = binder.getCoreBinder().getConnectionFactory().getPartitions("foo" + uniqueBindingId + ".0"); assertThat(partitions, hasSize(5)); binder.unbindProducers("foo" + uniqueBindingId + ".0"); binder.unbindConsumers("foo" + uniqueBindingId + ".0"); }
@Override public Spy spyOn(final String name) { String topic = KafkaMessageChannelBinder.escapeTopicName(name); KafkaTestBinder binderWrapper = (KafkaTestBinder) getBinder(); // Rewind offset, as tests will have typically already sent the messages we're trying to consume KafkaMessageListenerContainer messageListenerContainer = binderWrapper .getCoreBinder() .createMessageListenerContainer( new Properties(), UUID.randomUUID().toString(), 1, topic, OffsetRequest.EarliestTime()); final BlockingQueue<KafkaMessage> messages = new ArrayBlockingQueue<KafkaMessage>(10); messageListenerContainer.setMessageListener( new MessageListener() { @Override public void onMessage(KafkaMessage message) { messages.offer(message); } }); return new Spy() { @Override public Object receive(boolean expectNull) throws Exception { return messages.poll(expectNull ? 50 : 5000, TimeUnit.MILLISECONDS); } }; }