Skip to content

Commit

Permalink
Make webapp single page (only Ajax)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsyer committed Apr 27, 2011
1 parent 33c47b2 commit 5d22672
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,22 @@
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.amqp.rabbit.log4j.converter.AmqpLogMessageConverter;
import org.springframework.amqp.rabbit.log4j.listener.AmqpLogMessageConverter;
import org.springframework.amqp.rabbit.log4j.listener.AmqpLogMessageListener;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
* Configuration for Server.
*
* @author Tomas Lukosius
* @author Dave Syer
*/
@Configuration
@Import(PropertyPlaceholderConfiguration.class)
public class RabbitServerConfiguration {

/**
Expand All @@ -45,22 +49,31 @@ public class RabbitServerConfiguration {
public static String LOG_QUEUE_NAME = "app.log4j.demo";
public static String LOG_ALL_INFO_ROUTING_KEY = "#." + Level.INFO.toString();

@Value("${amqp.port:5672}")
private int port = 5672;

@Value("${amqp.username:guest}")
private String username = "guest";

@Value("${amqp.password:guest}")
private String password = "guest";

@Value("${amqp.vhost:/}")
private String virtualHost = "/";

@Value("${amqp.host:localhost}")
private String host = "localhost";

@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost");
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
connectionFactory.setVirtualHost(virtualHost);
connectionFactory.setPort(port);
return connectionFactory;
}

@Bean
public MessageConverter amqpLogMessageConverter() {
return new AmqpLogMessageConverter();
}

@Bean
public TopicExchange logExchange() {
return new TopicExchange(LOG_EXCHANGE_NAME, false, false);
Expand All @@ -83,7 +96,7 @@ public Queue queue() {

@Bean
public Binding binding() {
return BindingBuilder.from(queue()).to(logExchange()).with(LOG_ALL_INFO_ROUTING_KEY);
return BindingBuilder.bind(queue()).to(logExchange()).with(LOG_ALL_INFO_ROUTING_KEY);
}

@Bean
Expand All @@ -105,3 +118,15 @@ public SimpleMessageListenerContainer listenerContainer() {
return container;
}
}

/**
* Configuration for property placeholders (enables system and OS placeholders with <code>&#64;Value(${...})</code>).
* @author Dave Syer
*/
@Configuration
class PropertyPlaceholderConfiguration {
@Bean
public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
return new PropertyPlaceholderConfigurer();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* 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.log4j.web.domain;
package org.springframework.amqp.rabbit.log4j.listener;

import java.text.DateFormat;
import java.util.Date;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* 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.log4j.converter;
package org.springframework.amqp.rabbit.log4j.listener;

import java.util.Date;
import java.util.Map;
Expand All @@ -20,7 +20,6 @@
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.log4j.AmqpAppender;
import org.springframework.amqp.rabbit.log4j.web.domain.AmqpLogMessage;
import org.springframework.amqp.support.converter.AbstractMessageConverter;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.util.CollectionUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.Queue;
import java.util.concurrent.PriorityBlockingQueue;

import org.springframework.amqp.rabbit.log4j.web.domain.AmqpLogMessage;

/**
* @author tomas.lukosius@opencredo.com
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,33 @@
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.log4j.config.server.RabbitServerConfiguration;
import org.springframework.amqp.rabbit.log4j.listener.AmqpLogMessage;
import org.springframework.amqp.rabbit.log4j.listener.AmqpLogMessageListener;
import org.springframework.amqp.rabbit.log4j.web.domain.AmqpLogMessage;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
* @author tomas.lukosius@opencredo.com
* @author Tomas Lukosius
* @author Dave Syer
*
*/
@Controller
public class LogsController implements DisposableBean {
protected Log logger = LogFactory.getLog(this.getClass());

private static final String CURRENT_LOG_QUEUE = "CURRENT_LOG_QUEUE";
private static final String CURRENT_ROUTINGKEY = "CURRENT_ROUTINGKEY";

protected Log logger = LogFactory.getLog(this.getClass());

@Autowired
private TopicExchange exchange;
Expand All @@ -63,12 +59,9 @@ public class LogsController implements DisposableBean {
@Autowired
private AmqpLogMessageListener messageListener;

@RequestMapping("/logs")
@RequestMapping(value="/logs", method=RequestMethod.GET)
@ResponseBody
public List<AmqpLogMessage> logs(@RequestParam(required = false) Long timestamp, HttpSession session) {
session.setAttribute(CURRENT_LOG_QUEUE, RabbitServerConfiguration.LOG_QUEUE_NAME);
session.setAttribute(CURRENT_ROUTINGKEY, binding.getRoutingKey());

public List<AmqpLogMessage> logs(@RequestParam(required = false) Long timestamp) {
if (timestamp == null) {
timestamp = 0L;
}
Expand All @@ -82,26 +75,26 @@ public List<AmqpLogMessage> logs(@RequestParam(required = false) Long timestamp,
return list;
}

@RequestMapping("/bindQueue")
@RequestMapping(value="/binding", method=RequestMethod.POST)
@ResponseBody
public String addQueue(@RequestParam(required = true, value = "routingkey") String routingKey, HttpSession session) {
if (!StringUtils.hasText(routingKey)) {
return "Routing key expected";
}

public Binding addBinding(@RequestParam String routingKey) {
try {
admin.removeBinding(binding);
binding = BindingBuilder.from(logQueue).to(exchange).with(routingKey);
Binding binding = BindingBuilder.bind(logQueue).to(exchange).with(routingKey);
admin.declareBinding(binding);
this.binding = binding;
} catch (RuntimeException e) {
// TODO: create a new BindingResult object to carry back error message
logger.error("Failed to declare queue or bind it with exchage", e);
return e.getMessage();
}

session.setAttribute(CURRENT_LOG_QUEUE, RabbitServerConfiguration.LOG_QUEUE_NAME);
session.setAttribute(CURRENT_ROUTINGKEY, binding.getRoutingKey());
return this.binding;
}

return "Queue '" + logQueue.getName() + "' was binded with routing key '" + routingKey + "'";
@RequestMapping(value="/binding", method=RequestMethod.GET)
@ResponseBody
public Binding getBinding() {
return binding;
}

public void destroy() throws Exception {
Expand Down
Loading

0 comments on commit 5d22672

Please sign in to comment.