List of usage examples for org.springframework.integration.endpoint PollingConsumer setBeanFactory
@Override public void setBeanFactory(BeanFactory beanFactory)
From source file:com.apress.prospringintegration.endpoints.pollingconsumer.Main.java
public static void main(String[] args) { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext( "polling-consumer.xml"); applicationContext.start();/*from www .ja v a 2 s. c o m*/ ProblemReporter problemReporter = applicationContext.getBean(ProblemReporter.class); TicketMessageHandler ticketMessageHandler = applicationContext.getBean(TicketMessageHandler.class); TicketGenerator ticketGenerator = applicationContext.getBean(TicketGenerator.class); QueueChannel channel = applicationContext.getBean("ticketChannel", QueueChannel.class); // Define the polling consumer PollingConsumer ticketConsumer = new PollingConsumer(channel, ticketMessageHandler); ticketConsumer.setReceiveTimeout(RECEIVE_TIMEOUT); ticketConsumer.setBeanFactory(applicationContext); // Setup the poller using periodic trigger PeriodicTrigger periodicTrigger = new PeriodicTrigger(1000); periodicTrigger.setInitialDelay(5000); periodicTrigger.setFixedRate(false); PollerMetadata pollerMetadata = new PollerMetadata(); pollerMetadata.setTrigger(periodicTrigger); pollerMetadata.setMaxMessagesPerPoll(3); ticketConsumer.setPollerMetadata(pollerMetadata); // Starts the polling consumer in the other thread ticketConsumer.start(); // Generates messages and sends to the channel List<Ticket> tickets = ticketGenerator.createTickets(); while (true) { for (Ticket ticket : tickets) { problemReporter.openTicket(ticket); } } }
From source file:nhs.spring.integration.App.java
public static void main(String... args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "spring-integration-context.xml"); GameMessageHandler gameHandler = new GameMessageHandler(); GameGenerator gameGenerator = context.getBean(GameGenerator.class); context.start();//w w w .jav a 2 s . co m //MessageChannel input = context.getBean("input-channel", MessageChannel.class); //PollableChannel output = context.getBean("output-channel", PollableChannel.class); QueueChannel qChannel = context.getBean("game-channel", QueueChannel.class); PollingConsumer gameConsumer = new PollingConsumer(qChannel, gameHandler); gameConsumer.setReceiveTimeout(RECEIVE_TIMEOUT); gameConsumer.setBeanFactory(context); // Set up the poller using periodic trigger PeriodicTrigger periodicTrigger = new PeriodicTrigger(1000); periodicTrigger.setInitialDelay(5000); periodicTrigger.setFixedRate(false); PollerMetadata pollerMetadata = new PollerMetadata(); pollerMetadata.setTrigger(periodicTrigger); pollerMetadata.setMaxMessagesPerPoll(3); gameConsumer.setPollerMetadata(pollerMetadata); // Starts the polling consumer in the other thread gameConsumer.start(); Date today = new Date(); // Generates messages and sends to the channel Game game = gameGenerator.generateGame("League of legend", "Riot Games", today, "Tom", "Michael", "AOS"); qChannel.send(MessageBuilder.withPayload(game).build()); /* PublishSubscribeChannel pubsubChannel = null; pubsubChannel.subscribe(gameHandler); input.send(MessageBuilder.withPayload("Spring Integration / Hello NHS").build()); Message<?> reply = output.receive(); System.out.println("Received :" + reply); */ }
From source file:org.springframework.integration.config.ConsumerEndpointFactoryBean.java
private void initializeEndpoint() throws Exception { synchronized (this.initializationMonitor) { if (this.initialized) { return; }//from w w w . j av a 2 s .c o m MessageChannel channel = null; if (StringUtils.hasText(this.inputChannelName)) { Assert.isTrue(this.beanFactory.containsBean(this.inputChannelName), "no such input channel '" + this.inputChannelName + "' for endpoint '" + this.beanName + "'"); channel = this.beanFactory.getBean(this.inputChannelName, MessageChannel.class); } if (this.inputChannel != null) { channel = this.inputChannel; } Assert.state(channel != null, "one of inputChannelName or inputChannel is required"); if (channel instanceof SubscribableChannel) { Assert.isNull(this.pollerMetadata, "A poller should not be specified for endpoint '" + this.beanName + "', since '" + channel + "' is a SubscribableChannel (not pollable)."); this.endpoint = new EventDrivenConsumer((SubscribableChannel) channel, this.handler); } else if (channel instanceof PollableChannel) { PollingConsumer pollingConsumer = new PollingConsumer((PollableChannel) channel, this.handler); if (this.pollerMetadata == null) { this.pollerMetadata = PollerMetadata.getDefaultPollerMetadata(this.beanFactory); Assert.notNull(this.pollerMetadata, "No poller has been defined for endpoint '" + this.beanName + "', and no default poller is available within the context."); } pollingConsumer.setTaskExecutor(this.pollerMetadata.getTaskExecutor()); pollingConsumer.setTrigger(this.pollerMetadata.getTrigger()); pollingConsumer.setAdviceChain(this.pollerMetadata.getAdviceChain()); pollingConsumer.setMaxMessagesPerPoll(this.pollerMetadata.getMaxMessagesPerPoll()); pollingConsumer.setErrorHandler(this.pollerMetadata.getErrorHandler()); pollingConsumer.setReceiveTimeout(this.pollerMetadata.getReceiveTimeout()); pollingConsumer.setTransactionSynchronizationFactory( this.pollerMetadata.getTransactionSynchronizationFactory()); pollingConsumer.setBeanClassLoader(beanClassLoader); pollingConsumer.setBeanFactory(beanFactory); this.endpoint = pollingConsumer; } else { throw new IllegalArgumentException("unsupported channel type: [" + channel.getClass() + "]"); } this.endpoint.setBeanName(this.beanName); this.endpoint.setBeanFactory(this.beanFactory); this.endpoint.setAutoStartup(this.autoStartup); this.endpoint.afterPropertiesSet(); this.initialized = true; } }