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
|
package notification.server.listeners;
import java.io.IOException;
import java.util.Properties;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.log4j.Logger;
import notification.clients.NotificationClient;
import notification.server.NotificationServer;
import notification.utils.ListenersTools;
@MessageDriven(name="SubscriptionQueueProcessor",
activationConfig={
@ActivationConfigProperty(
propertyName="destinationType",
propertyValue="javax.jms.Queue"),
@ActivationConfigProperty(
propertyName="destination",
propertyValue="queue/SubscriptionQueue")
}
)
public class SubscriptionListenerWithQueue implements MessageListener{
/** Logger. */
private static final Logger logger = Logger.getLogger(SubscriptionListenerWithQueue.class);
public SubscriptionListenerWithQueue() throws IOException {
logger.debug(this.getClass().getName() + "constructor");
}
@Override
public void onMessage(Message message) {
TextMessage mess = (TextMessage) message;
try {
String textMessage = mess.getText();
String[] args = textMessage.split(",");
String[] pojos = ListenersTools.getPojos(args);
NotificationServer server = NotificationServer.getServer();
NotificationClient client = ListenersTools.getClient(args[0], Integer.parseInt(args[1]));
boolean ok = server.subscribe(client, pojos);
if (ok)
sendAcknowledgement(client, pojos,true);
} catch (JMSException e) {
logger.error(this.getClass().getName() + " "+e.getMessage());
}
}
private void sendAcknowledgement(NotificationClient client, String[] pojos,
boolean b) {
Properties prop=new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
prop.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
prop.put(Context.PROVIDER_URL,"jnp://localhost:1099");
Context context = null;
try {
context = new InitialContext(prop);
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ConnectionFactory factory = (ConnectionFactory) context.lookup("ConnectionFactory");
Connection connection = factory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = (Destination)context.lookup("queue/AcknowledgementQueue");
MessageProducer producer = session.createProducer(destination);
TextMessage message = session.createTextMessage("Bien reçu");
producer.send(message);
connection.close();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} |
Partager