Spring JMS


Initialize project with ActiveQM Client

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-activemq</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>	

Enable JMS configuration

@Configuration
@EnableJms
public class IntegrationConfiguration {

}

Add integration layer controller

@Component
public class EmployeeJms {

    @Autowired
    private EmployeeService service;

    @JmsListener(destination = "employee-info-request")
    @SendTo("employee-info-response")
    public String getDepartment(String employeeId) {
        return service.getDepartment(employeeId);
    }
}

Simulation for business

@Service
public class EmployeeService {

    private static final Logger LOGGER = LoggerFactory
        .getLogger(EmployeeService.class);

    public String getDepartment(String employeeId) {
        LOGGER.info("Begin get department info of employee [{}]", employeeId);
        simulateQuery(employeeId, 3);
        LOGGER.info("End get department info of employee [{}]", employeeId);
        return "Technology";
    }

    private void simulateQuery(String employeeId, int processTime) {
        try {
            TimeUnit.SECONDS.sleep(processTime);
        }
        catch (InterruptedException e) {
        }
    }

}

Configure to point to Message Broker

spring:
    application:
        name: spring-jms
    profiles.active: default
    activemq:
        in-memory: false
        broker-url: tcp://activemq-dev:61616
        user: admin
        password: admin
    jms:
        listener:
            auto-startup: true
            concurrency: 3
            max-concurrency: 3

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.