- Structure
- Constants.java
package org.blog.test.constant; public class Constants { public static final String MAILBOX_DESTINATION = "mailbox-destination"; }
- JmsReceiver.java
package org.blog.test.jms; import java.io.File; import org.blog.test.constant.Constants; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Component; import org.springframework.util.FileSystemUtils; @Component public class JmsReceiver { @Autowired ConfigurableApplicationContext context; @JmsListener(destination = Constants.MAILBOX_DESTINATION, containerFactory = "jmsListenerContainerFactory") public void receiveMessage(String message) { System.out.println("Received : " + message); context.close(); FileSystemUtils.deleteRecursively(new File("activemq-data")); } }
- TestApplication.java
package org.blog.test; import java.io.File; import javax.jms.ConnectionFactory; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Session; import org.blog.test.constant.Constants; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.jms.annotation.EnableJms; import org.springframework.jms.config.JmsListenerContainerFactory; import org.springframework.jms.config.SimpleJmsListenerContainerFactory; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; import org.springframework.util.FileSystemUtils; @SpringBootApplication @EnableJms public class TestApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } @Autowired private JmsTemplate jmsTemplate; @Bean JmsListenerContainerFactory jmsListenerContainerFactory(ConnectionFactory connectionFactory) { SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory(); factory.setConnectionFactory(connectionFactory); return factory; } @Override public void run(String... arg0) throws Exception { FileSystemUtils.deleteRecursively(new File("activemq-data")); MessageCreator messageCreator = new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { return session.createTextMessage("ping"); } }; System.out.println("sending a new message"); jmsTemplate.send(Constants.MAILBOX_DESTINATION, messageCreator); } }
- pom.xml
4.0.0 org.blog.test jms-project 0.1.0 org.springframework.boot spring-boot-starter-parent 1.3.2.RELEASE org.springframework.boot spring-boot-starter org.springframework spring-jms org.apache.activemq activemq-broker org.projectlombok lombok 1.16.6 org.springframework.boot spring-boot-maven-plugin
- Execution Result
sending a new message 2016-02-08 12:32:52.082 INFO 4396 --- [ main] org.blog.test.TestApplication : Started TestApplication in 2.642 seconds (JVM running for 3.073) Received : ping
original source : https://spring.io/guides/gs/messaging-jms/
댓글 없음 :
댓글 쓰기