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
|
/**
*
*/
package com.citizenweb.utilz;
import java.util.Hashtable;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.Topic;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
/**
* @author Fred
*
*/
public class NotificationManager {
Context context = null;
ConnectionFactory jmsFactory = null;
Connection connection = null;
String factoryName = "ConnectionFactory";
String destName = null;
Destination dest = null;
Session session = null;
MessageProducer sender = null;
MessageConsumer receiver = null;
public NotificationManager(){
}
private void connect(){
try{
//create the JNDI initial context
System.out.println("CONNECT");
Hashtable<String,String> properties = new Hashtable<String,String>();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.exolab.jms.jndi.InitialContextFactory");
properties.put(Context.PROVIDER_URL, "tcp://localhost:3035/");
properties.put(Context.SECURITY_CREDENTIALS, "openjms");
properties.put(Context.SECURITY_PRINCIPAL, "admin");
context = new InitialContext(properties);
System.out.println(context.toString());
//lookup the ConnectionFactory
jmsFactory=(ConnectionFactory)context.lookup(factoryName);
System.out.println(jmsFactory.toString());
//create connection
connection = jmsFactory.createConnection();
System.out.println(connection.toString());
//create the session
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
System.out.println(session.toString());
}catch(JMSException e){
System.out.println("<< ERREUR in " + this.getClass().getSimpleName()+" | Méthode : connect()");
e.printStackTrace();
}catch(NamingException e){
System.out.println("<< ERREUR in " + this.getClass().getSimpleName()+" | Méthode : connect()");
e.printStackTrace();
}
}
public Boolean createTopicUser(String topicUser){
System.out.println("CREATE TOPIC");
boolean done = false;
try {
connect();
connection.start();
System.out.println("TOPIC USER = " + topicUser);
session.createTopic(topicUser);
done = true;
System.out.println("DONE = " + done);
System.out.println("CONNECT META DATA : " + connection.getMetaData());
} catch (JMSException e) {
System.out.println("<< ERREUR in " + this.getClass().getSimpleName()+" | Méthode : createTopicUser()");
e.printStackTrace();
} finally {
if(context != null){
try {
context.close();
} catch(NamingException e){
System.out.println("<< ERREUR in " + this.getClass().getSimpleName()+" | Méthode : createTopicUser()");
e.printStackTrace();
}
}
if(connection != null){
try {
connection.close();
} catch (JMSException e) {
System.out.println("<< ERREUR in " + this.getClass().getSimpleName()+" | Méthode : createTopicUser()");
e.printStackTrace();
}
}
}
return done;
}
public Boolean subscribeTopic(String topic, String idUser){
boolean done = false;
try {
connect();
connection.start();
dest = (Destination) context.lookup(topic);
session.createDurableSubscriber((Topic) dest, idUser);
done = true;
} catch (NamingException e) {
System.out.println("<< ERREUR in " + this.getClass().getSimpleName()+" | Méthode : createTopicUser()");
e.printStackTrace();
} catch (JMSException e) {
System.out.println("<< ERREUR in " + this.getClass().getSimpleName()+" | Méthode : createTopicUser()");
e.printStackTrace();
} finally {
if(context != null){
try {
context.close();
} catch(NamingException e){
System.out.println("<< ERREUR in " + this.getClass().getSimpleName()+" | Méthode : createTopicUser()");
e.printStackTrace();
}
}
if(connection != null){
try {
connection.close();
} catch (JMSException e) {
System.out.println("<< ERREUR in " + this.getClass().getSimpleName()+" | Méthode : createTopicUser()");
e.printStackTrace();
}
}
}
return done;
}
} |
Partager