ajout TP4
This commit is contained in:
parent
c9d7acaedc
commit
12949804d9
63
TP4/HelloTopic.java
Normal file
63
TP4/HelloTopic.java
Normal file
|
@ -0,0 +1,63 @@
|
|||
|
||||
import javax.jms.Connection;
|
||||
import javax.jms.ConnectionFactory;
|
||||
import javax.jms.Destination;
|
||||
import javax.jms.Message;
|
||||
import javax.jms.MessageConsumer;
|
||||
import javax.jms.MessageListener;
|
||||
import javax.jms.MessageProducer;
|
||||
import javax.jms.Session;
|
||||
import javax.jms.TextMessage;
|
||||
|
||||
import org.apache.activemq.ActiveMQConnection;
|
||||
import org.apache.activemq.ActiveMQConnectionFactory;
|
||||
|
||||
public class HelloTopic {
|
||||
|
||||
private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
|
||||
private static String subject = "MyTopic";
|
||||
|
||||
public static void main(String argv[]) {
|
||||
|
||||
try {
|
||||
|
||||
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
|
||||
|
||||
Connection connection = connectionFactory.createConnection();
|
||||
connection.start();
|
||||
|
||||
Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
|
||||
|
||||
Destination destination = session.createTopic(subject);
|
||||
|
||||
MessageProducer producer = session.createProducer(destination);
|
||||
MessageConsumer consumer = session.createConsumer(destination);
|
||||
|
||||
MessageListener listener = new MessageListener() {
|
||||
public void onMessage(Message msg) {
|
||||
try {
|
||||
TextMessage textmsg = (TextMessage)msg;
|
||||
System.out.println("received : "+textmsg.getText());
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
consumer.setMessageListener(listener);
|
||||
|
||||
TextMessage message = session.createTextMessage("Hello welcome to ActiveMQ!");
|
||||
producer.send(message);
|
||||
System.out.println("Sentage '" + message.getText() + "'");
|
||||
Thread.sleep(1000);
|
||||
|
||||
connection.close();
|
||||
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
143
TP4/Irc.java
Normal file
143
TP4/Irc.java
Normal file
|
@ -0,0 +1,143 @@
|
|||
|
||||
import java.awt.Button;
|
||||
import java.awt.Color;
|
||||
import java.awt.FlowLayout;
|
||||
import java.awt.Frame;
|
||||
import java.awt.TextArea;
|
||||
import java.awt.TextField;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Vector;
|
||||
|
||||
import javax.jms.Connection;
|
||||
import javax.jms.ConnectionFactory;
|
||||
import javax.jms.Message;
|
||||
import javax.jms.MessageConsumer;
|
||||
import javax.jms.MessageListener;
|
||||
import javax.jms.MessageProducer;
|
||||
import javax.jms.Session;
|
||||
import javax.jms.StreamMessage;
|
||||
import javax.jms.Topic;
|
||||
|
||||
import org.apache.activemq.ActiveMQConnection;
|
||||
import org.apache.activemq.ActiveMQConnectionFactory;
|
||||
|
||||
public class Irc {
|
||||
public static TextArea text;
|
||||
public static TextField data;
|
||||
public static Frame frame;
|
||||
|
||||
public static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
|
||||
public static String subject = "MyQueue";
|
||||
|
||||
public static Vector<String> users = new Vector<String>();
|
||||
public static String myName;
|
||||
|
||||
public static ConnectionFactory connectionFactory;
|
||||
public static Connection connection;
|
||||
public static Session session;
|
||||
public static MessageConsumer consumer;
|
||||
public static MessageProducer producer;
|
||||
public static Topic topic;
|
||||
|
||||
public static void main(String argv[]) {
|
||||
|
||||
if (argv.length != 1) {
|
||||
System.out.println("java Irc <name>");
|
||||
return;
|
||||
}
|
||||
myName = argv[0];
|
||||
|
||||
// creation of the GUI
|
||||
frame=new Frame();
|
||||
frame.setLayout(new FlowLayout());
|
||||
|
||||
text=new TextArea(10,55);
|
||||
text.setEditable(false);
|
||||
text.setForeground(Color.red);
|
||||
frame.add(text);
|
||||
|
||||
data=new TextField(55);
|
||||
frame.add(data);
|
||||
|
||||
Button write_button = new Button("write");
|
||||
write_button.addActionListener(new writeListener());
|
||||
frame.add(write_button);
|
||||
|
||||
Button connect_button = new Button("connect");
|
||||
connect_button.addActionListener(new connectListener());
|
||||
frame.add(connect_button);
|
||||
|
||||
Button who_button = new Button("who");
|
||||
who_button.addActionListener(new whoListener());
|
||||
frame.add(who_button);
|
||||
|
||||
Button leave_button = new Button("leave");
|
||||
leave_button.addActionListener(new leaveListener());
|
||||
frame.add(leave_button);
|
||||
|
||||
frame.setSize(470,300);
|
||||
text.setBackground(Color.black);
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
/* allow to print something in the window */
|
||||
public static void print(String msg) {
|
||||
try {
|
||||
text.append(msg+"\n");
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// action invoked when the "write" button is clicked
|
||||
class writeListener implements ActionListener {
|
||||
public void actionPerformed (ActionEvent ae) {
|
||||
try {
|
||||
System.out.println("write button pressed");
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// action invoked when the "connect" button is clicked
|
||||
class connectListener implements ActionListener {
|
||||
public void actionPerformed (ActionEvent ae) {
|
||||
try {
|
||||
System.out.println("connect button pressed");
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// action invoked when the "who" button is clicked
|
||||
class whoListener implements ActionListener {
|
||||
public void actionPerformed (ActionEvent ae) {
|
||||
try {
|
||||
System.out.println("who button pressed");
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// action invoked when the "leave" button is clicked
|
||||
class leaveListener implements ActionListener {
|
||||
public void actionPerformed (ActionEvent ae) {
|
||||
try {
|
||||
System.out.println("leave button pressed");
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
BIN
TP4/archi.jpg
Normal file
BIN
TP4/archi.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 64 KiB |
134
TP4/tp-jms.html
Normal file
134
TP4/tp-jms.html
Normal file
|
@ -0,0 +1,134 @@
|
|||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="CONTENT-TYPE" content="text/html;
|
||||
charset=windows-1252">
|
||||
<title></title>
|
||||
<meta name="GENERATOR" content="OpenOffice.org 1.1.4 (Win32)">
|
||||
<meta name="CREATED" content="20051026;11210625">
|
||||
<meta name="CHANGEDBY" content="Daniel Hagimont">
|
||||
<meta name="CHANGED" content="20051112;3545775">
|
||||
<meta name="ProgId" content="Word.Document">
|
||||
<meta name="Originator" content="Microsoft Word 10">
|
||||
<meta name="Template" content="C:\PROGRAM FILES\MICROSOFT
|
||||
OFFICE\OFFICE\html.dot">
|
||||
<style>
|
||||
<!--
|
||||
P { color: #000000 }
|
||||
P.msonormal { margin-bottom: 0cm; font-family: "Times New Roman" }
|
||||
A:link { color: #0000ff }
|
||||
A:visited { color: #800080 }
|
||||
-->
|
||||
</style>
|
||||
</head>
|
||||
<body dir="ltr" vlink="#800080" text="#000000" link="#0000ff"
|
||||
lang="fr-FR">
|
||||
<p align="center"><br>
|
||||
<br>
|
||||
</p>
|
||||
<p style="margin-bottom: 0cm;" align="center"><font
|
||||
style="font-size: 20pt;" size="5"><b>Cours Systèmes Communicants</b></font></p>
|
||||
<p align="center"><font style="font-size: 20pt;" size="5"><b>ENSEEIHT
|
||||
|
||||
|
||||
|
||||
|
||||
– Département Informatique – 2ième année</b></font></p>
|
||||
<p align="center"><font style="font-size: 20pt;" size="5"><b>TP JMS</b></font></p>
|
||||
<p style="margin-bottom: 0cm;" align="center"><font size="3">Daniel
|
||||
Hagimont</font></p>
|
||||
<p style="margin-bottom: 0cm;" align="center"><font size="3">Daniel.Hagimont@enseeiht.fr</font></p>
|
||||
<p class="msonormal" style="margin-left: 5.08cm; margin-bottom:
|
||||
0.5cm;"><br>
|
||||
</p>
|
||||
<p class="msonormal" style="margin-left: 1.27cm; margin-bottom:
|
||||
0.5cm;"><a name="_Toc432321016"></a> <font face="Arial"><font
|
||||
style="font-size: 13pt;" size="3"><b>1.
|
||||
|
||||
|
||||
|
||||
|
||||
Objectif</b></font></font></p>
|
||||
<p style="margin-left: 1.27cm;">L'objectif de ce TP est de
|
||||
programmer une application répartie en utilisant une implantation
|
||||
de l'interface JMS vue en cours.</p>
|
||||
<p class="msonormal" style="margin-left: 1.27cm; margin-bottom:
|
||||
0.5cm;"> <font face="Arial"><font style="font-size: 13pt;"
|
||||
size="3"><b>2. Spécification</b></font></font></p>
|
||||
<p style="margin-left: 1.27cm;">L'objectif est la gestion d'un <i>Forum</i>
|
||||
de discussion pouvant faire intervenir un nombre quelconque
|
||||
d'intervenants utilisant une application <i>Irc</i>. Chaque <i>Irc</i>
|
||||
peut émettre des messages qui sont diffusés à l'ensemble des <i>Irc</i>.
|
||||
Les messages ne sont pas mémorisés par le <i>Forum</i>. Seuls les
|
||||
<i>Irc</i> présents sur le <i>Forum</i> reçoivent les messages
|
||||
émis. Pour émettre, un <i>Irc</i> doit être connecté au <i>Forum</i>.
|
||||
</p>
|
||||
<p style="margin-left: 1.27cm;">Nous considérons les opérations
|
||||
suivantes :</p>
|
||||
<ul>
|
||||
<ul>
|
||||
<li>
|
||||
<p>Connect : permet à un <i>Irc</i> de se connecter au <i>Forum</i>.</p>
|
||||
</li>
|
||||
<li>
|
||||
<p>Leave : permet à un <i>Irc</i> de se déconnecter du <i>Forum</i>.</p>
|
||||
</li>
|
||||
<li>
|
||||
<p>Write : permet à un <i>Irc</i> d'émettre un message.</p>
|
||||
</li>
|
||||
<li>
|
||||
<p>Who : permet à un <i>Irc</i> de connaître l'ensemble des
|
||||
intervenants connectés au <i>Forum</i>.</p>
|
||||
</li>
|
||||
</ul>
|
||||
</ul>
|
||||
<p style="margin-left: 1.27cm;">Nous utilisons JMS en mode
|
||||
Publish/Subscribe.</p>
|
||||
<p style="margin-left: 1.27cm;"><br>
|
||||
<img alt="archi" src="archi.jpg" width="576" height="231"><br>
|
||||
</p>
|
||||
<p class="msonormal" style="margin-left: 1.27cm; margin-bottom:
|
||||
0.5cm;"><br clear="left">
|
||||
<br>
|
||||
</p>
|
||||
<p style="margin-left: 1.27cm;">L’application est composée du
|
||||
fichier <a href="Irc.java">Irc.java</a> qui inclut l'interface
|
||||
graphique de l'application, que vous devez compléter pour faire
|
||||
marcher l'application avec JMS.</p>
|
||||
Un petit
|
||||
exemple (<a href="HelloTopic.java">HelloTopic.java</a>) vous est
|
||||
donné pour démarrer.
|
||||
<p style="margin-left: 1.27cm;">Pour utiliser activeMQ
|
||||
(l'implantation de JMS) :<br>
|
||||
</p>
|
||||
<ul style="margin-left: 40px;">
|
||||
<li>récupérez apache-active-MQ</li>
|
||||
<ul>
|
||||
<li>wget
|
||||
http://sd-127206.dedibox.fr/hagimont/software/apache-activemq-5.14.0-bin.tar.gz<br>
|
||||
</li>
|
||||
</ul>
|
||||
<li>décompresser l'archive (apache-activemq-5.14.0-bin.tar.gz)
|
||||
dans un répertoire <activemq_path></li>
|
||||
<li>pour lancer un daemon activeMQ :
|
||||
<activemq_path>/bin/activemq start (et stop pour
|
||||
l'arrêter)<br>
|
||||
</li>
|
||||
<li>si vous utilisez eclipse, le jar à ajouter dans votre
|
||||
BuildPath est : <activemq_path>/activemq-all-5.14.0.jar</li>
|
||||
<li>si vous voulez compiler (ou exécuter) en dehors d'eclipse :
|
||||
javac -cp .:<activemq_path>/lib/* <fichier source
|
||||
java></li>
|
||||
</ul>
|
||||
<p style="margin-left: 1.27cm;">0) testez l'exemple HelloTopic
|
||||
ci-dessus<br>
|
||||
1) commencez par coder les opérations de connexion, emission et
|
||||
réception de messages<br>
|
||||
2) implantez une solution gérant la liste des intervenants
|
||||
connectés<br>
|
||||
3) rajouter des connexions "durables" : si vous quitter Irc et que
|
||||
vous vous reconnectez plus tard, vous recevez les messages émis
|
||||
pendant votre absence.<br>
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in a new issue