Skip to content
This repository was archived by the owner on Dec 15, 2021. It is now read-only.

Sample project for issue SPR-13278 #100

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions SPR-13278/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>net.lkrnac.book.eiws</groupId>
<artifactId>auto-acknowledge-listener-duplicate-fail</artifactId>
<version>0.0.2-SNAPSHOT</version>
<packaging>jar</packaging>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<!-- jms -->
<dependency>
<groupId>org.hornetq</groupId>
<artifactId>hornetq-jms-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hornetq</artifactId>
</dependency>

<!-- productivity -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.4</version>
</dependency>

<!-- db -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>

<!-- test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.8</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package net.lkrnac.book.eiws.chapter06.text;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class JmsApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(JmsApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.lkrnac.book.eiws.chapter06.text;

import javax.jms.ConnectionFactory;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.SimpleJmsListenerContainerFactory;

@Configuration
public class JmsConfiguration {
@Bean
public SimpleJmsListenerContainerFactory jmsListenerContainerFactory(
ConnectionFactory connectionFactory) {
SimpleJmsListenerContainerFactory factory =
new SimpleJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
return factory;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package net.lkrnac.book.eiws.chapter06.text;

import javax.jms.Session;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class SimpleMessageListener {
private SimpleService simpleService;
private boolean errorSimulated = false;

@Autowired
public SimpleMessageListener(SimpleService simpleService) {
super();
this.simpleService = simpleService;
}

@JmsListener(destination = "ExpiryQueue")
public void readMessage(String message, Session session) {
simpleService.processText(message);
postprocess(message);
}

private void postprocess(String message) {
// simulate error
if (!errorSimulated) {
errorSimulated = true;
throw new IllegalArgumentException(message);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package net.lkrnac.book.eiws.chapter06.text;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class SimpleMessageSender {
private static final String SIMPLE_MESSAGE = "simple message";
private JmsTemplate jmsTemplate;

@Autowired
public SimpleMessageSender(JmsTemplate jmsTemplate) {
super();
this.jmsTemplate = jmsTemplate;
}

// @PostConstruct
@Scheduled(initialDelay = 1000, fixedRate = Long.MAX_VALUE)
public void send() {
log.info("Sending message: {}", SIMPLE_MESSAGE);
jmsTemplate.convertAndSend("ExpiryQueue", SIMPLE_MESSAGE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package net.lkrnac.book.eiws.chapter06.text;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class SimpleRepository {
private final JdbcTemplate jdbcTemplate;
private static final String SELECT_COUNT =
"select count(*) from TEXT_TABLE where text = ?";

@Autowired
public SimpleRepository(JdbcTemplate jdbcTemplate) {
super();
this.jdbcTemplate = jdbcTemplate;
}

@PostConstruct
public void initDbTable() {
jdbcTemplate.execute("drop table TEXT_TABLE if exists");
jdbcTemplate.execute("create table TEXT_TABLE(TEXT varchar(30))");
}

public void persistText(String text) {
jdbcTemplate.update("insert into TEXT_TABLE values (?)", text);
}

public boolean containsText(String text) {
long count = jdbcTemplate.queryForObject(SELECT_COUNT, Long.class, text);
return count != 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.lkrnac.book.eiws.chapter06.text;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class SimpleService {
private final SimpleRepository simpleRepository;

@Autowired
public SimpleService(SimpleRepository simpleRepository) {
super();
this.simpleRepository = simpleRepository;
}

public void processText(String text) {
log.info("Process Message: {}", text);
simpleRepository.persistText(text);
}

public boolean isProcessed(String text) {
return simpleRepository.containsText(text);
}
}
3 changes: 3 additions & 0 deletions SPR-13278/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
spring.hornetq.mode=embedded
spring.hornetq.embedded.enabled=true
spring.hornetq.embedded.queues=ExpiryQueue
13 changes: 13 additions & 0 deletions SPR-13278/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<configuration>

<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />

<root level="DEBUG">
<appender-ref ref="CONSOLE" />
</root>

<logger name="org.springframework.jms.listener" level="DEBUG"/>
<logger name="org.springframework.jdbc" level="DEBUG"/>
<logger name="org.hornetq.core.client" level="DEBUG"/>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package net.lkrnac.book.eiws.chapter06.text;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.Test;

@SpringApplicationConfiguration(classes = JmsApplication.class)
public class JmsApplicationTests extends AbstractTestNGSpringContextTests {
private static final String SELECT_COUNT =
"select count(*) from TEXT_TABLE where text = ?";

private static final String MESSAGE_TEXT = "simple message";

@Autowired
private JdbcTemplate jdbcTemplate;

@Test(timeOut = 3000)
public void testJms() throws Exception {
// GIVEN: Spring configuration

// WHEN
Thread.sleep(2000);

// THEN
long count =
jdbcTemplate.queryForObject(SELECT_COUNT, Long.class, MESSAGE_TEXT);
Assert.assertEquals(count, 2);
}
}