Refactor spring-jms code samples

This commit is contained in:
Grzegorz Piwowarek 2016-10-07 10:39:07 +02:00
parent 1296f2e40f
commit 64d5a03863
6 changed files with 116 additions and 121 deletions

View File

@ -90,12 +90,13 @@
<module>spring-hibernate3</module>
<module>spring-hibernate4</module>
<module>spring-jpa</module>
<module>spring-jms</module>
<module>spring-katharsis</module>
<module>spring-mockito</module>
<module>spring-mvc-java</module>
<module>spring-mvc-no-xml</module>
<module>spring-mvc-xml</module>
<module>spring-mvc-tiles</module>
<module>spring-mvc-tiles</module>
<module>spring-openid</module>
<module>spring-protobuf</module>
<module>spring-quartz</module>

View File

@ -1,23 +1,23 @@
package com.baeldung.spring.jms;
public class Employee {
private String name;
private Integer age;
public Employee(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
public String toString() {
return "Person: name(" + name + "), age(" + age + ")";
}
}
package com.baeldung.spring.jms;
public class Employee {
private String name;
private Integer age;
public Employee(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
public String toString() {
return "Person: name(" + name + "), age(" + age + ")";
}
}

View File

@ -1,17 +1,16 @@
package com.baeldung.spring.jms;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.activemq.broker.BrokerFactory;
import org.apache.activemq.broker.BrokerService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SampleJMSExample {
public static void main(String[] args) throws URISyntaxException, Exception {
BrokerService broker = BrokerFactory.createBroker(new URI("broker:(tcp://localhost:61616)"));
broker.start();
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
}
}
package com.baeldung.spring.jms;
import org.apache.activemq.broker.BrokerFactory;
import org.apache.activemq.broker.BrokerService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.net.URI;
public class SampleJMSExample {
public static void main(String[] args) throws Exception {
BrokerService broker = BrokerFactory.createBroker(new URI("broker:(tcp://localhost:61616)"));
broker.start();
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
}
}

View File

@ -1,32 +1,28 @@
package com.baeldung.spring.jms;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.Session;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
public class SampleJmsMessageSender {
private JmsTemplate jmsTemplate;
private Queue queue;
public void setConnectionFactory(ConnectionFactory cf) {
this.jmsTemplate = new JmsTemplate(cf);
}
public void setQueue(Queue queue) {
this.queue = queue;
}
public void simpleSend() {
this.jmsTemplate.send(this.queue, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage("hello queue world");
}
});
}
package com.baeldung.spring.jms;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import javax.jms.*;
public class SampleJmsMessageSender {
private JmsTemplate jmsTemplate;
private Queue queue;
public void createJmsTemplate(ConnectionFactory cf) {
this.jmsTemplate = new JmsTemplate(cf);
}
public void setQueue(Queue queue) {
this.queue = queue;
}
public void simpleSend() {
this.jmsTemplate.send(this.queue, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage("hello queue world");
}
});
}
}

View File

@ -1,23 +1,23 @@
package com.baeldung.spring.jms;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class SampleListener implements MessageListener {
private String textMessage;
public void onMessage(Message message) {
if (message instanceof TextMessage) {
try {
textMessage = ((TextMessage) message).getText();
} catch (JMSException ex) {
throw new RuntimeException(ex);
}
} else {
throw new IllegalArgumentException("Message Error");
}
}
package com.baeldung.spring.jms;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class SampleListener implements MessageListener {
private String textMessage;
public void onMessage(Message message) {
if (message instanceof TextMessage) {
try {
textMessage = ((TextMessage) message).getText();
} catch (JMSException ex) {
throw new RuntimeException(ex);
}
} else {
throw new IllegalArgumentException("Message Error");
}
}
}

View File

@ -1,27 +1,26 @@
package com.baeldung.spring.jms;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.jms.support.converter.MessageConversionException;
import org.springframework.jms.support.converter.MessageConverter;
public class SampleMessageConverter implements MessageConverter {
public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException {
Employee person = (Employee) object;
MapMessage message = session.createMapMessage();
message.setString("name", person.getName());
message.setInt("age", person.getAge());
return message;
}
public Object fromMessage(Message message) throws JMSException, MessageConversionException {
MapMessage mapMessage = (MapMessage) message;
Employee person = new Employee(mapMessage.getString("name"), mapMessage.getInt("age"));
return person;
}
}
package com.baeldung.spring.jms;
import org.springframework.jms.support.converter.MessageConversionException;
import org.springframework.jms.support.converter.MessageConverter;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;
public class SampleMessageConverter implements MessageConverter {
public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException {
Employee person = (Employee) object;
MapMessage message = session.createMapMessage();
message.setString("name", person.getName());
message.setInt("age", person.getAge());
return message;
}
public Object fromMessage(Message message) throws JMSException, MessageConversionException {
MapMessage mapMessage = (MapMessage) message;
return new Employee(mapMessage.getString("name"), mapMessage.getInt("age"));
}
}