NSY102 Conception de logiciels Intranet : Patrons et Canevas

this.runner.start();. } public void run(){ for( Channel output : destinations){ try{ output.sendMessage(message);. }catch(RemoteException e){. //e.printStackTrace();. }.
17KB taille 30 téléchargements 396 vues
NSY102 Conception de logiciels Intranet : Patrons et Canevas. Session de Juin 2007 Une idée…

Question1 (5 points) : Patron Observateur / Pattern Observer public class Liste implements SujetObservé, Iterable { private List contenu = new ArrayList(); private List observateurs = new ArrayList();

public void ajouter( T t ){ contenu.add(t); notifierAuxObservateurs(); } public void retirer(T t) { contenu.remove(t); notifierAuxObservateurs(); } public boolean estPrésent( T t ){ return contenu.contains(t); }

public Iterator iterator() { return contenu.iterator(); } public void ajouterUnObservateur( Observateur obs ) { if(!observateurs.contains(obs)) observateurs.add( obs ); } public void retirerUnObservateur( Observateur obs ) { observateurs.remove( obs ); } //alerte les observateurs d'une modification de this private void notifierAuxObservateurs() { for(Observateur obs : observateurs){ obs.miseAJour(this); } }

Question2 (10 points) : Patron Diffuseur / Pattern RecipienList alias StoreAndForward Question2-1) 2 points public class Receiver implements Channel{ private String name; private Message received; public Receiver(String name) throws RemoteException{ this.name = name;

1/6

} public Receiver(Registry registry, String name) throws RemoteException{ this(name); Remote stub = UnicastRemoteObject.exportObject(this,0); registry.rebind(name,stub); } public void sendMessage( Message message) throws RemoteException{ System.out.println("message.getSource() : " + message.getSource()); System.out.println("message.getMessage() : " + message.getMessage()); this.received = message; } public Message getReceived(){ return received; } public String toString(){ return "Receiver : " + name; } }

Question2-2) 5 points public class RecipientListImpl implements RecipientList{ private Map store; private String name; public RecipientListImpl(String name) throws RemoteException{ this.name = name; this.store = new HashMap(); } public RecipientListImpl(Registry registry, String name) throws RemoteException{ this(name); Remote stub = UnicastRemoteObject.exportObject(this,0); registry.rebind(name,stub); } public synchronized void sendMessage( Message message) throws RemoteException{ InputChannel input = message.getSource(); Channel[] destinations = store.get(input); if(destinations != null) new RouterWorkThread( message, destinations); } public void addRoute(InputChannel source, Channel[] destinations) throws RemoteException{ store.put(source, destinations); } public String getName(){ return this.name; } public String toString(){ String res = "RecipientList:"; for( InputChannel key : store.keySet()){ res = res + ""; return res; } private class RouterWorkThread implements Runnable{ private Message message; private Channel[] destinations; private Thread runner; public RouterWorkThread(Message message, Channel[] destinations){ this.message = message; this.destinations = destinations; this.runner = new Thread(this); this.runner.start(); } public void run(){ for( Channel output : destinations){ try{ output.sendMessage(message); }catch(RemoteException e){ //e.printStackTrace(); } } } }

}

Question2-3) 2 points public static void main(String[] args) { System.setSecurityManager(new RMISecurityManager()); Registry registry =null; try{ registry = LocateRegistry.createRegistry(1100); }catch(Exception e){ //fail(e.getMessage()); e.printStackTrace(); } try{ registry = LocateRegistry.getRegistry(1100); RecipientList recipient = new RecipientListImpl(registry, "test2"); Receiver Receiver Receiver Receiver

r1 r2 r3 r4

= = = =

new new new new

Receiver(registry, Receiver(registry, Receiver(registry, Receiver(registry,

"A"); "B"); "C"); "D");

InputChannel in = new InputString("in");

RecipientList recipient_stub = (RecipientList)registry.lookup("test2"); Channel r1_stub = (Channel)registry.lookup("A"); Channel r2_stub = (Channel)registry.lookup("B"); Channel r3_stub = (Channel)registry.lookup("C");

3/6

Channel r4_stub = (Channel)registry.lookup("D"); recipient_stub.addRoute(in, new Channel[]{r1_stub,r2_stub,r3_stub,r4_stub}); Message msg = new Message(in,"test1"); recipient_stub.sendMessage(msg); Thread.sleep(1000); }catch(Exception e){ e.printStackTrace(); }finally{ System.exit(0); // radical } } }

Question2-4) 1 point Nous devons garantir la même valeur de hashCode entre deux machines virtuelles, ce qui n’est pas le cas lors de l’usage de hashCode héritée de la classe Object

Question3 (5 points) : Architecture JMS & Chat Question3-2) 4 point /* extrait de http://forums.devx.com/showthread.php?t=137396 */ public class ChatJMS implements javax.jms.MessageListener { private TopicSession pubSession; private TopicSession subSession; private TopicPublisher publisher; private TopicConnection connection; /* Constructor. Establish JMS publisher and subscriber */ public ChatJMS() throws Exception { // Obtain a JNDI connection Hashtable props = new Hashtable(); props.put(Context.INITIAL_CONTEXT_FACTORY, "org.exolab.jms.jndi.InitialContextFactory"); props.put(Context.PROVIDER_URL, "tcp://localhost:3035/"); InitialContext jndi = new InitialContext(props); // Look up a JMS connection factory TopicConnectionFactory conFactory = (TopicConnectionFactory) jndi.lookup("JmsTopicConnectionFactory"); // Create a JMS connection TopicConnection connection = conFactory.createTopicConnection(); // Create two JMS session objects TopicSession pubSession = connection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE); TopicSession subSession = connection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE); // Look up a JMS topic Topic chatTopic = (Topic) jndi.lookup("anytopic");

4/6

// Create a JMS publisher and subscriber TopicPublisher publisher = pubSession.createPublisher(chatTopic); TopicSubscriber subscriber = subSession.createSubscriber(chatTopic); // Set a JMS message listener subscriber.setMessageListener(this); // Intialize the Chat application set(connection, pubSession, subSession, publisher); // Start the JMS connection; allows messages to be delivered connection.start(); } /* Initialize the instance variables */ public void set(TopicConnection con, TopicSession pubSess, TopicSession subSess, TopicPublisher pub) { this.connection = con; this.pubSession = pubSess; this.subSession = subSess; this.publisher = pub; } /* Receive message from topic subscriber */ public void onMessage(Message message) { try { TextMessage textMessage = (TextMessage) message; String text = textMessage.getText(); System.out.println(text); } catch (JMSException jmse) { jmse.printStackTrace(); } } /* Create and send message using topic publisher */ protected void writeMessage(String text) throws JMSException { TextMessage message = pubSession.createTextMessage(text); publisher.publish(message); } /* Close the JMS connection */ public void close() throws JMSException { connection.close(); } /* Run the Chat client */ public static void main(String[] args) { try { ChatJMS chat = new ChatJMS(); // Read from command line BufferedReader commandLine = new java.io.BufferedReader(new InputStreamReader(System.in)); // Loop until the word "exit" is typed while (true) { String s = commandLine.readLine(); if (s.equalsIgnoreCase("exit")) {

5/6

chat.close(); // close down connection System.exit(0); // exit program } else { chat.writeMessage(s); } } } catch (Exception e) { e.printStackTrace(); } } }

6/6