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
| /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mdb;
import java.util.Properties;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import persistence.Facture;
import teset.*;
/**
*
* @author laleye
*/
@MessageDriven(mappedName = "jms/topic", activationConfig = {
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
@ActivationConfigProperty(propertyName = "subscriptionDurability", propertyValue = "Durable"),
@ActivationConfigProperty(propertyName = "clientId", propertyValue = "NewMessageBean"),
@ActivationConfigProperty(propertyName = "subscriptionName", propertyValue = "NewMessageBean")
})
public class NewMessageBean implements MessageListener {
public NewMessageBean() {
}
@Override
public void onMessage(Message msg) {
TextMessage tm;
try {
if (msg instanceof TextMessage){
tm = (TextMessage) msg;
int val=msg.getJMSPriority();
String text = tm.getText();
System.out.println(text);
String[] msgextract=text.split(",");
// NewSessionBean md=new NewSessionBean();
// md.CreerFacture(Integer.parseInt(msgextract[0]), msgextract[1], msgextract[2], msgextract[3], msgextract[4], msgextract[5], Double.parseDouble(msgextract[6]));
// sendPrintingMessage(tm) ;
envoi(tm);
}
else {
System.out.println("mauvais format");}
}catch(Exception e){e.printStackTrace();}
}
public void envoi(Message m) throws NamingException, JMSException{
Properties props = new Properties();
props.setProperty("java.naming.factory.initial",
"com.sun.enterprise.naming.SerialInitContextFactory");
props.setProperty("java.naming.factory.url.pkgs",
"com.sun.enterprise.naming");
props.setProperty("java.naming.factory.state",
"com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
// optional. Defaults to localhost. Only needed if web server is running
// on a different host than the appserver
props.setProperty("org.omg.CORBA.ORBInitialHost", "hote");
// optional. Defaults to 3700. Only needed if target orb port is not 3700.
props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
InitialContext ctx = new InitialContext(props);
// Context ctx = new InitialContext();
// 1: recherche d'une fabrique de connection via JNDI
TopicConnectionFactory factory=(TopicConnectionFactory) ctx.lookup("topicfact");
// QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("topicfact");
// 2: Utilisation de la fabrique de connexions pour créer une connexion JMS
TopicConnection connection=factory.createTopicConnection();
// QueueConnection connection = factory.createQueueConnection();
// 3: Utilisation de la connection pour créer une session
TopicSession session = connection.createTopicSession(false,TopicSession.AUTO_ACKNOWLEDGE);
// QueueSession session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
// 4: Recherche du sujet (topic) via JNDI
Topic topic=(javax.jms.Topic) ctx.lookup("jms/Mtopic");
// javax.jms.Queue queue = (javax.jms.Queue) ctx.lookup("jms/Mtopic");
// 5: Création d'un producteur de message
MessageProducer sender=session.createPublisher(topic);
//TopicSubscriber topicSubscriber = session.createSubscriber(topic);
//MessageConsumer cons=session.createConsumer(topic);
connection.start();
//
//
// // 6: Creation d'un message texte et publication
// TextMessage msg = session.createTextMessage();
// // for (int i=0;i<2;i++){
// msg.setText("2,SAIZONOU,Romaine,OUEME,Porto-novo,tokkpota,56789.0");
sender.send(m);
// sender.send(msg);
//sender.send(session.createMessage());
// }
//session.close();
//connection.close();
//
// // topicSubscriber.close();
sender.close();
// // connection.stop();
session.close();
connection.close();
System.exit(0);
}
} |
Partager