From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- .../src/examples/messaging/LogBean.java | 33 ++++++++++++++++++ .../src/examples/messaging/LogClient.java | 38 ++++++++++++++++++++ .../examples/messaging/META-INF/sun-ejb-jar.xml | 20 +++++++++++ .../src/examples/messaging/PoisonBean.java | 40 ++++++++++++++++++++++ .../Gerald Examples/src/examples/messaging/Readme | 11 ++++++ .../src/examples/messaging/build.xml | 30 ++++++++++++++++ .../src/examples/messaging/dd/LogBean.java | 22 ++++++++++++ .../src/examples/messaging/dd/LogClient.java | 34 ++++++++++++++++++ .../src/examples/messaging/dd/META-INF/ejb-jar.xml | 19 ++++++++++ .../examples/messaging/dd/META-INF/sun-ejb-jar.xml | 13 +++++++ .../src/examples/messaging/dd/build.xml | 30 ++++++++++++++++ 11 files changed, 290 insertions(+) create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/LogBean.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/LogClient.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/META-INF/sun-ejb-jar.xml create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/PoisonBean.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/Readme create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/build.xml create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/dd/LogBean.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/dd/LogClient.java create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/dd/META-INF/ejb-jar.xml create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/dd/META-INF/sun-ejb-jar.xml create mode 100644 Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/dd/build.xml (limited to 'Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging') diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/LogBean.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/LogBean.java new file mode 100644 index 0000000..c8ff738 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/LogBean.java @@ -0,0 +1,33 @@ +package examples.messaging; + +import javax.jms.*; +import javax.ejb.*; +import javax.annotation.PreDestroy; + +@MessageDriven(activationConfig = { + @ActivationConfigProperty(propertyName = "destinationType", + propertyValue = "javax.jms.Topic") + }) +public class LogBean implements MessageListener { + + public LogBean() { + System.out.println("LogBean created"); + } + + public void onMessage(Message msg) { + if (msg instanceof TextMessage) { + TextMessage tm = (TextMessage) msg; + try { + String text = tm.getText(); + System.out.println("Received new message : " + text); + } catch (JMSException e) { + e.printStackTrace(); + } + } + } + + @PreDestroy + public void remove() { + System.out.println("LogBean destroyed."); + } +} \ No newline at end of file diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/LogClient.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/LogClient.java new file mode 100644 index 0000000..483c0f1 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/LogClient.java @@ -0,0 +1,38 @@ +package examples.messaging; + +import javax.jms.*; +import javax.naming.InitialContext; + +public class LogClient { + + public static void main(String[] args) throws Exception { + // Initialize JNDI + InitialContext ctx = new InitialContext(System.getProperties()); + + // 1: Lookup connection factory + TopicConnectionFactory factory = + (TopicConnectionFactory) ctx.lookup("jms/TopicConnectionFactory"); + + // 2: Use connection factory to create JMS connection + TopicConnection connection = factory.createTopicConnection(); + + // 3: Use connection to create a session + TopicSession session = + connection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE); + + // 4: Lookup destination + Topic topic = (Topic)ctx.lookup("jms/Topic"); + + // 5: Create a message publisher + TopicPublisher publisher = session.createPublisher(topic); + + // 6: Create and publish a message + TextMessage msg = session.createTextMessage(); + msg.setText("This is a test message."); + publisher.send(msg); + + // finish + publisher.close(); + System.out.println("Message published. Please check application server's console to see the response from MDB."); + } +} \ No newline at end of file diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/META-INF/sun-ejb-jar.xml b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/META-INF/sun-ejb-jar.xml new file mode 100644 index 0000000..6389cbf --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/META-INF/sun-ejb-jar.xml @@ -0,0 +1,20 @@ + + + + LogBean + + LogBean + jms/Topic + + jms/TopicConnectionFactory + + + + PoisonBean + jms/Topic + + jms/TopicConnectionFactory + + + + diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/PoisonBean.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/PoisonBean.java new file mode 100644 index 0000000..2bdfcba --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/PoisonBean.java @@ -0,0 +1,40 @@ +package examples.messaging; + +import javax.jms.*; +import javax.ejb.*; +import javax.annotation.PreDestroy; +import javax.annotation.Resource; + +@MessageDriven(activationConfig = +{ @ActivationConfigProperty(propertyName = "destinationType", + propertyValue = "javax.jms.Topic") }) +public class PoisonBean implements MessageListener { + + @Resource + private MessageDrivenContext ctx; + + public PoisonBean() { + System.out.println("PoisonBean created"); + } + + public void onMessage(Message msg) { + try { + System.out.println("Received msg " + msg.getJMSMessageID()); + + // Let’s sleep a little bit so that we don’t + // see rapid fire re-sends of the message. + Thread.sleep(3000); + + // We could either throw a system exception here or + // manually force a rollback of the transaction. + ctx.setRollbackOnly(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @PreDestroy + public void remove() { + System.out.println("PoisonBean destroyed."); + } +} \ No newline at end of file diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/Readme b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/Readme new file mode 100644 index 0000000..2800cc7 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/Readme @@ -0,0 +1,11 @@ +The messagin examples require that JMS connection factories and +destination resources be created before deploying and running +the samples. + +More specifically, you must create a javax.jms.TopicConnectionFactory +resource with an JNDI name jms/TopicConnectionFactory, and a +javax.jms.Topic destination resource with JNDI name jms/Topic. In +Sun RI/Glassfish, the destination resource must have an additional +property that links it with a physical destination. To provide that, +add the property "name" with value "Topic" to that resource in the admin +GUI. diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/build.xml b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/build.xml new file mode 100644 index 0000000..140b55e --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/build.xml @@ -0,0 +1,30 @@ + + ]> + + + + + + + + + + + + + + + + &include; + + + + + + + + + + diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/dd/LogBean.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/dd/LogBean.java new file mode 100644 index 0000000..89364d9 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/dd/LogBean.java @@ -0,0 +1,22 @@ +package examples.messaging.dd; + +import javax.jms.*; + +public class LogBean implements MessageListener { + + public LogBean() { + } + + public void onMessage(Message msg) { + if (msg instanceof TextMessage) { + TextMessage tm = (TextMessage) msg; + try { + String text = tm.getText(); + System.err.println("Received new message : " + text); + } catch (JMSException e) { + e.printStackTrace(); + } + } + } + +} \ No newline at end of file diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/dd/LogClient.java b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/dd/LogClient.java new file mode 100644 index 0000000..14b6008 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/dd/LogClient.java @@ -0,0 +1,34 @@ +package examples.messaging.dd; + +import javax.jms.Session; +import javax.jms.TextMessage; +import javax.jms.Topic; +import javax.jms.TopicConnection; +import javax.jms.TopicConnectionFactory; +import javax.jms.TopicPublisher; +import javax.jms.TopicSession; +import javax.naming.InitialContext; + +public class LogClient { + + public static void main(String[] args) throws Exception { + InitialContext ctx = new InitialContext(System.getProperties()); + + Topic topic = (Topic) ctx.lookup("jms/Topic"); + TopicConnectionFactory factory = + (TopicConnectionFactory) ctx + .lookup("jms/TopicConnectionFactory"); + TopicConnection connection = factory.createTopicConnection(); + TopicSession session = connection.createTopicSession(false, + Session.AUTO_ACKNOWLEDGE); + + TopicPublisher publisher = session.createPublisher(topic); + + // create and publish a message + TextMessage msg = session.createTextMessage(); + msg.setText("This is a test message."); + publisher.publish(msg); + + System.out.println("Message published. Please check application server's console to see the response from MDB."); + } +} \ No newline at end of file diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/dd/META-INF/ejb-jar.xml b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/dd/META-INF/ejb-jar.xml new file mode 100644 index 0000000..92cee32 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/dd/META-INF/ejb-jar.xml @@ -0,0 +1,19 @@ + + + + + LogBeanDD + examples.messaging.dd.LogBean + javax.jms.MessageListener + Bean + javax.jms.Topic + + + + destinationType + javax.jms.Topic + + + + + \ No newline at end of file diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/dd/META-INF/sun-ejb-jar.xml b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/dd/META-INF/sun-ejb-jar.xml new file mode 100644 index 0000000..85caf26 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/dd/META-INF/sun-ejb-jar.xml @@ -0,0 +1,13 @@ + + + + LogBeanDD + + LogBeanDD + jms/Topic + + jms/TopicConnectionFactory + + + + diff --git a/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/dd/build.xml b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/dd/build.xml new file mode 100644 index 0000000..b6b0013 --- /dev/null +++ b/Master/Reference Architectures and Patterns/EJB 3.0 Code/Gerald Examples/src/examples/messaging/dd/build.xml @@ -0,0 +1,30 @@ + + ]> + + + + + + + + + + + + + + + + &include; + + + + + + + + + + -- cgit v1.2.3