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-hibernate3</module>
<module>spring-hibernate4</module> <module>spring-hibernate4</module>
<module>spring-jpa</module> <module>spring-jpa</module>
<module>spring-jms</module>
<module>spring-katharsis</module> <module>spring-katharsis</module>
<module>spring-mockito</module> <module>spring-mockito</module>
<module>spring-mvc-java</module> <module>spring-mvc-java</module>
<module>spring-mvc-no-xml</module> <module>spring-mvc-no-xml</module>
<module>spring-mvc-xml</module> <module>spring-mvc-xml</module>
<module>spring-mvc-tiles</module> <module>spring-mvc-tiles</module>
<module>spring-openid</module> <module>spring-openid</module>
<module>spring-protobuf</module> <module>spring-protobuf</module>
<module>spring-quartz</module> <module>spring-quartz</module>

View File

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

View File

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

View File

@ -1,32 +1,28 @@
package com.baeldung.spring.jms; package com.baeldung.spring.jms;
import javax.jms.ConnectionFactory; import org.springframework.jms.core.JmsTemplate;
import javax.jms.JMSException; import org.springframework.jms.core.MessageCreator;
import javax.jms.Message;
import javax.jms.Queue; import javax.jms.*;
import javax.jms.Session;
public class SampleJmsMessageSender {
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator; private JmsTemplate jmsTemplate;
private Queue queue;
public class SampleJmsMessageSender {
public void createJmsTemplate(ConnectionFactory cf) {
private JmsTemplate jmsTemplate; this.jmsTemplate = new JmsTemplate(cf);
private Queue queue; }
public void setConnectionFactory(ConnectionFactory cf) { public void setQueue(Queue queue) {
this.jmsTemplate = new JmsTemplate(cf); this.queue = queue;
} }
public void setQueue(Queue queue) { public void simpleSend() {
this.queue = queue; this.jmsTemplate.send(this.queue, new MessageCreator() {
} public Message createMessage(Session session) throws JMSException {
return session.createTextMessage("hello queue world");
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; package com.baeldung.spring.jms;
import javax.jms.JMSException; import javax.jms.JMSException;
import javax.jms.Message; import javax.jms.Message;
import javax.jms.MessageListener; import javax.jms.MessageListener;
import javax.jms.TextMessage; import javax.jms.TextMessage;
public class SampleListener implements MessageListener { public class SampleListener implements MessageListener {
private String textMessage; private String textMessage;
public void onMessage(Message message) { public void onMessage(Message message) {
if (message instanceof TextMessage) { if (message instanceof TextMessage) {
try { try {
textMessage = ((TextMessage) message).getText(); textMessage = ((TextMessage) message).getText();
} catch (JMSException ex) { } catch (JMSException ex) {
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }
} else { } else {
throw new IllegalArgumentException("Message Error"); throw new IllegalArgumentException("Message Error");
} }
} }
} }

View File

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