Skip to content

Instantly share code, notes, and snippets.

<filter>
<filter-name>SpringSparkFilter</filter-name>
<filter-class>com.zhentao.sparkjava.example.SpringSparkFilter</filter-class>
<init-param>
<param-name>applicationClass</param-name>
<param-value>com.zhentao.sparkjava.example.MySparkApplication</param-value>
</init-param>
<init-param>
<!-- Configuration locations must consist of one or more comma-delimited fully-qualified @Configuration classes -->
<param-name>springConfigLocation</param-name>
@Override
public void init() {
get("/my-resource", (req, res) -> {
return exampleService.getDate();
});
}
@Override
protected SparkApplication getApplication(FilterConfig filterConfig) throws ServletException {
Class<?>[] configClasses = getConfigClasses(filterConfig);
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(configClasses)) {
String applicationClassName = filterConfig.getInitParameter(APPLICATION_CLASS_PARAM);
try {
return (SparkApplication) context.getBean(Class.forName(applicationClassName));
} catch (ClassNotFoundException e) {
throw new ServletException(e);
public void sendMessage() {
Employee employee = new Employee();
employee.setId(counter.incrementAndGet());
employee.setName("name-" + employee.getId());
rabbitTemplate.convertAndSend(employee);
}
public void handleMessage(Employee employee) {
LOG.info("handle employee with id {} and name {}", employee.getId(), employee.getName());
}
@Bean
public SimpleMessageListenerContainer listenerContainer() {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory());
container.setQueueNames(this.employeeQueueName);
container.setMessageListener(new MessageListenerAdapter(new MessageHandler(), jsonMessageConverter()));
return container;
}
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setAddresses("localhost:5673,localhost:5672");
return connectionFactory;
}
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import javax.sql.DataSource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class EmbeddedDataSourceConfig implements DataSourceConfig {
@Override
@Bean(destroyMethod="shutdown")