Switch servlet config to use XML

This commit is contained in:
dsyer
2011-03-13 17:39:35 +00:00
parent afa5759dcf
commit 7b64f99825
2 changed files with 57 additions and 126 deletions

View File

@@ -1,111 +0,0 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.amqp.rabbit.stocks.config.servlet;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.amqp.rabbit.stocks.config.AbstractStockAppRabbitConfiguration;
import org.springframework.amqp.rabbit.stocks.gateway.RabbitStockServiceGateway;
import org.springframework.amqp.rabbit.stocks.gateway.StockServiceGateway;
import org.springframework.amqp.rabbit.stocks.web.QuoteController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Configures RabbitTemplate and creates the Trader queue and binding for the client.
*
* @author Mark Pollack
* @author Mark Fisher
* @author Dave Syer
*/
@Configuration
public class RabbitServletConfiguration extends AbstractStockAppRabbitConfiguration {
@Value("${stocks.quote.pattern}")
private String marketDataRoutingKey;
@Autowired
private QuoteController quoteController;
// Create the Queue definitions that write up the Message listener container
/**
* The client's template will by default send to the exchange defined
* in {@link org.springframework.amqp.rabbit.config.AbstractRabbitConfiguration#rabbitTemplate()}
* with the routing key {@link AbstractStockAppRabbitConfiguration#STOCK_REQUEST_QUEUE_NAME}
* <p>
* The default exchange will delivery to a queue whose name matches the routing key value.
*/
@Override
public void configureRabbitTemplate(RabbitTemplate rabbitTemplate) {
rabbitTemplate.setRoutingKey(STOCK_REQUEST_QUEUE_NAME);
}
@Bean
public StockServiceGateway stockServiceGateway() {
RabbitStockServiceGateway gateway = new RabbitStockServiceGateway();
gateway.setRabbitTemplate(rabbitTemplate());
gateway.setDefaultReplyToQueue(traderJoeQueue());
return gateway;
}
@Bean
public SimpleMessageListenerContainer messageListenerContainer() {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory());
container.setQueues(marketDataQueue(), traderJoeQueue());
container.setMessageListener(messageListenerAdapter());
return container;
}
@Bean
public MessageListenerAdapter messageListenerAdapter() {
return new MessageListenerAdapter(quoteController, jsonMessageConverter());
}
// Broker Configuration
@Bean
public Queue marketDataQueue() {
return amqpAdmin().declareQueue();
}
/**
* Binds to the market data exchange. Interested in any stock quotes.
*/
@Bean
public Binding marketDataBinding() {
return BindingBuilder.from(marketDataQueue()).to(marketDataExchange()).with(marketDataRoutingKey);
}
/**
* This queue does not need a binding, since it relies on the default exchange.
*/
@Bean
public Queue traderJoeQueue() {
return amqpAdmin().declareQueue();
}
}

View File

@@ -1,23 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- pick up rabbit broker configuration -->
<context:component-scan base-package="org.springframework.amqp.rabbit.stocks.config.servlet"/>
<context:property-placeholder location="classpath:/client.properties"/>
<bean class="org.springframework.amqp.rabbit.stocks.web.QuoteController">
<property name="stockServiceGateway" ref="stockServiceGateway"/>
<bean id="stockServiceGateway"
class="org.springframework.amqp.rabbit.stocks.gateway.RabbitStockServiceGateway">
<property name="rabbitTemplate">
<bean class="org.springframework.amqp.rabbit.core.RabbitTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="routingKey" value="app.stock.request" />
<property name="messageConverter" ref="jsonMessageConverter"/>
</bean>
</property>
<property name="defaultReplyToQueue" ref="tradeQueue" />
</bean>
<mvc:resources location="file:./src/main/resources/static/,classpath:/static/" mapping="static/**" />
<rabbit:queue name="tradeQueue" />
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<rabbit:queue name="marketDataQueue" />
<rabbit:fanout-exchange name="broadcast.responses">
<rabbit:bindings>
<rabbit:binding queue="tradeQueue" />
</rabbit:bindings>
</rabbit:fanout-exchange>
<rabbit:topic-exchange name="app.stock.marketdata">
<rabbit:bindings>
<rabbit:binding queue="marketDataQueue" pattern="${stocks.quote.pattern}" />
</rabbit:bindings>
</rabbit:topic-exchange>
<rabbit:listener-container
connection-factory="connectionFactory" message-converter="jsonMessageConverter">
<rabbit:listener ref="quoteController" method="handleMessage"
queue-names="marketDataQueue,tradeQueue" />
</rabbit:listener-container>
<bean class="org.springframework.amqp.rabbit.core.RabbitAdmin">
<constructor-arg ref="connectionFactory" />
</bean>
<bean id="connectionFactory"
class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory" />
<context:property-placeholder location="classpath:/client.properties" />
<bean id="quoteController"
class="org.springframework.amqp.rabbit.stocks.web.QuoteController">
<property name="stockServiceGateway" ref="stockServiceGateway" />
</bean>
<mvc:resources location="file:./src/main/resources/static/,classpath:/static/"
mapping="static/**" />
<mvc:default-servlet-handler />
<mvc:annotation-driven />
</beans>