1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
package jms01;
//~--- JDK imports ------------------------------------------------------------
import java.util.Enumeration;
import java.util.Properties;
import javax.jms.ConnectionMetaData;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.naming.Context;
import javax.naming.InitialContext;
/*
lIBRAIRIE SL4J obligatoire et slf4j-log4j12.jar et log4j.jar
Ne pas oublier de lancer ACTIVEMQ
Utilisation de MesageMap en emission, puis en reception
jndi.properties
## ---------------------------------------------------------------------------
java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory
# use the following property to configure the default connector
java.naming.provider.url = vm://localhost
# use the following property to specify the JNDI name the connection factory
# should appear as.
#connectionFactoryNames = connectionFactory, queueConnectionFactory, topicConnectionFactry
# register some queues in JNDI using the form
# queue.[jndiName] = [physicalName]
queue.MyQueue = example.MyQueue
# register some topics in JNDI using the form
# topic.[jndiName] = [physicalName]
topic.MyTopic = example.MyTopic
*/
public class SimpleQueueSenderJNDI
{
private static int nbMessagesTest = 5;
public static void main(String[] args) throws Exception {
//Part1 Send Messages
Properties props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
props.setProperty(Context.PROVIDER_URL, "tcp://localhost:61616");
javax.naming.Context ctx = new InitialContext(props); // lookup the connection factory
// lookup an existing topic
javax.jms.Topic mytopic = (javax.jms.Topic) ctx.lookup("MyTopic");
javax.jms.TopicConnectionFactory factory = (javax.jms.TopicConnectionFactory) ctx.lookup("ConnectionFactory"); // create a new TopicConnection for pub/sub messaging
javax.jms.TopicConnection topicConn = factory.createTopicConnection(); // getTopicConnection();
System.out.println(topicConn); // output this : ActiveMQConnection {id=ID:PCOUASXPS-3725-1310894474296-0:1,clientId=null,started=false}
// -- retrieve information describing the connection
ConnectionMetaData connInfo = topicConn.getMetaData();
// getJMSXPropertyNames
Enumeration propNames = connInfo.getJMSXPropertyNames();
topicConn.start();
// create a new TopicSession for the client
javax.jms.TopicSession session = topicConn.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
// Debut envoi
TopicPublisher publisher = session.createPublisher(mytopic);
for (int i = 0; i < nbMessagesTest; i++) {
// finally, create and publish a message:
MapMessage message = session.createMapMessage();
message.setString("Subject", "Subject Hello World " + i);
message.setString("Content", "Content Hello World " + i);
publisher.publish(message);
System.out.println("Message " + i + " envoyé id=" + message.getJMSMessageID());
}
session.close();
// close the topic connection
topicConn.close();
ctx.close();
System.out.println("Envoi terminé");
// -------------------------------------------------------------------------------------------------------------------------------------
//Thread.sleep(5000); //On attend 5 secondes
// Part2 Reception Message
//____________________________________________________________
props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
props.setProperty(Context.PROVIDER_URL, "tcp://localhost:61616");
ctx = new InitialContext(props); // lookup the connection factory
mytopic = (javax.jms.Topic) ctx.lookup("MyTopic"); // error is from here
factory = (javax.jms.TopicConnectionFactory) ctx.lookup("ConnectionFactory"); // create a new TopicConnection for pub/sub messaging
topicConn = factory.createTopicConnection(); // getTopicConnection();
System.out.println(topicConn); // output this : ActiveMQConnection {id=ID:PCOUASXPS-3725-1310894474296-0:1,clientId=null,started=false}
//
topicConn.start();
// create a new TopicSession for the client
session = topicConn.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
// Reception
//
javax.jms.TopicSubscriber topicSubscriber = session.createSubscriber(mytopic); // receive message
Message msg;
boolean isOn = true;
int j = 0;
String firstID = "",
lastID = "";
while (isOn) {
System.out.println("Boucle de reception " + j);
// receive the message
msg = topicSubscriber.receive(); //Indefiniment
//msg = topicSubscriber.receive(1000); // tIMEOUT APRES UNE SECONDE
lastID = msg.getJMSMessageID();
if (j == 0) {
firstID = msg.getJMSMessageID();
}
if (msg instanceof MapMessage) {
System.out.println("Correlation ID: " + msg.getJMSCorrelationID());
System.out.println("Message Priority: " + msg.getJMSPriority());
System.out.println("Redelivered: " + msg.getJMSRedelivered());
for (Enumeration propertyNames = msg.getPropertyNames(); propertyNames.hasMoreElements(); ) {
String name = propertyNames.nextElement().toString();
Object obj = msg.getObjectProperty(name);
System.out.println("Property name: " + name);
System.out.println("Property value: " + obj);
}
System.out.println("Read Message: " + ((MapMessage) msg).getString("Subject"));
System.out.println("Read Message: " + ((MapMessage) msg).getString("Content"));
System.out.println("Read Message id: " + lastID);
} else {
isOn = false;
}
j++;
}
System.out.println("consumed messages " + firstID + "-" + lastID);
System.out.println("byby");
// close the topic connection
topicConn.close();
}
} |
Partager