Skip to content

Commit

Permalink
updated the web app to support profiles, as well.
Browse files Browse the repository at this point in the history
  • Loading branch information
joshlong committed Sep 13, 2012
1 parent e81d7b1 commit 62012ac
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 30 deletions.
Expand Up @@ -53,6 +53,7 @@ public StockSymbolLookup lookupSymbol(String symbol) throws Throwable {
JsonNode node = new ObjectMapper().readTree(response);
return convertJsonNodeInToSymbolLookup(node);
}

private StockSymbolLookup convertJsonNodeInToSymbolLookup(JsonNode jsonNode) throws Throwable {
Number id = jsonNode.get("id").getValueAsLong();
Double changeWhileOpen = jsonNode.get("c").getValueAsDouble();
Expand Down
Expand Up @@ -34,13 +34,9 @@ private Long randomLong() {
return new Random().nextLong();
}

private StockSymbolLookup fabricateForSymbol(String ticker, String exchange) {
return new StockSymbolLookup(randomLong(), randomDouble(), ticker,
StringUtils.hasText(exchange) ? exchange : "NYSE", randomDouble(), randomDouble(), randomDouble());
}

@Override
public StockSymbolLookup lookupSymbol(String symbol) throws Throwable {
return fabricateForSymbol(symbol, null);
return new StockSymbolLookup(randomLong(), randomDouble(), symbol,
StringUtils.hasText(null) ? null : "NYSE", randomDouble(), randomDouble(), randomDouble());
}
}
Expand Up @@ -24,6 +24,7 @@
import org.cloudfoundry.runtime.service.messaging.RabbitServiceCreator;
import org.cloudfoundry.workers.common.config.CloudRabbitConnectionFactoryConfiguration;
import org.cloudfoundry.workers.common.config.LocalRabbitConnectionFactoryConfiguration;
import org.cloudfoundry.workers.common.config.RabbitConnectionFactoryConfiguration;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
Expand All @@ -45,6 +46,8 @@
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.util.Assert;

import javax.inject.Inject;

/**
* Configures the service's gateway client which in turn communicates with the
* remote service through a Spring Integration gateway implementation.
Expand All @@ -58,66 +61,69 @@ public class ClientConfiguration {

private String tickers = "tickers";

@Autowired
@Inject
private RabbitConnectionFactoryConfiguration rabbitConnectionFactoryConfiguration ;

@Inject
private Environment environment;

@Bean
public RabbitTemplate amqpTemplate() {
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory());
public RabbitTemplate amqpTemplate() throws Throwable {
RabbitTemplate rabbitTemplate = new RabbitTemplate( rabbitConnectionFactoryConfiguration.connectionFactory());
rabbitTemplate.setMessageConverter(mc());
return rabbitTemplate;
}

@Bean
public RabbitTransactionManager amqpTransactionManager() {
return new RabbitTransactionManager(this.connectionFactory());
public RabbitTransactionManager amqpTransactionManager() throws Throwable {
return new RabbitTransactionManager(rabbitConnectionFactoryConfiguration.connectionFactory());
}

@Bean
public MessageConverter mc() {
return new JsonMessageConverter();
}

@Bean
public ConnectionFactory connectionFactory() {

CloudEnvironment cloudEnvironment = this.cloudEnvironment();
Collection<RabbitServiceInfo> rabbitServiceInfoList = cloudEnvironment
.getServiceInfos(RabbitServiceInfo.class);
Assert.isTrue(rabbitServiceInfoList.size() > 0,
"the rabbitService infos collection should be > 0");
RabbitServiceInfo rabbitServiceInfo = rabbitServiceInfoList.iterator()
.next();
RabbitServiceCreator rabbitServiceCreator = new RabbitServiceCreator();
return rabbitServiceCreator.createService(rabbitServiceInfo);
}
//
// @Bean
// public ConnectionFactory connectionFactory() {
//
// CloudEnvironment cloudEnvironment = this.cloudEnvironment();
// Collection<RabbitServiceInfo> rabbitServiceInfoList = cloudEnvironment
// .getServiceInfos(RabbitServiceInfo.class);
// Assert.isTrue(rabbitServiceInfoList.size() > 0,
// "the rabbitService infos collection should be > 0");
// RabbitServiceInfo rabbitServiceInfo = rabbitServiceInfoList.iterator()
// .next();
// RabbitServiceCreator rabbitServiceCreator = new RabbitServiceCreator();
// return rabbitServiceCreator.createService(rabbitServiceInfo);
// }

@Bean
public CloudEnvironment cloudEnvironment() {
return new CloudEnvironment();
}

@Bean
public AmqpAdmin amqpAdmin() {
return new RabbitAdmin(this.connectionFactory());
public AmqpAdmin amqpAdmin() throws Throwable {
return new RabbitAdmin( rabbitConnectionFactoryConfiguration.connectionFactory());
}

@Bean
public Queue customerQueue() {
public Queue customerQueue() throws Throwable {
Queue q = new Queue(this.tickers);
amqpAdmin().declareQueue(q);
return q;
}

@Bean
public DirectExchange customerExchange() {
public DirectExchange customerExchange() throws Throwable {
DirectExchange directExchange = new DirectExchange(tickers);
this.amqpAdmin().declareExchange(directExchange);
return directExchange;
}

@Bean
public Binding marketDataBinding() {
public Binding marketDataBinding() throws Throwable {
return BindingBuilder.bind(customerQueue()).to(customerExchange())
.with(this.tickers);
}
Expand Down
@@ -0,0 +1,45 @@
package org.cloudfoundry.workers.stocks.web;

import org.cloudfoundry.runtime.env.CloudEnvironment;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

/**
* this class is called before the DispatcherServlet machinery is created,
* and gives us a chance to influence the {@link org.springframework.context.ApplicationContext}
* profiles.
*
* @author Josh Long
*/
public class StockWebApplicationContextInitializer implements ApplicationContextInitializer<AnnotationConfigWebApplicationContext> {

private CloudEnvironment cloudEnvironment = new CloudEnvironment();

private boolean isCloudFoundry() {
return cloudEnvironment.isCloudFoundry();
}

@Override
public void initialize(AnnotationConfigWebApplicationContext applicationContext) {


String profile;
if (isCloudFoundry()) {
profile = "cloud";
} else {
profile = "local";
}

applicationContext.getEnvironment().setActiveProfiles(profile);


Class<?>[] configs = {WebMvcConfiguration.class};
String[] basePkgs = new String[configs.length];
int i = 0;
for (Class<?> pkg : configs)
basePkgs[i++] = pkg.getPackage().getName();

applicationContext.scan(basePkgs);
applicationContext.refresh();
}
}
Expand Up @@ -18,6 +18,10 @@
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>
<param-name>contextInitializerClasses</param-name>
<param-value>org.cloudfoundry.workers.stocks.web.StockWebApplicationContextInitializer</param-value>
</init-param>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
Expand Down

0 comments on commit 62012ac

Please sign in to comment.