Gli elementi fondamentali di un`applicazione JMS sono

Transcript

Gli elementi fondamentali di un`applicazione JMS sono
1
JMS
Gli elementi fondamentali di un’applicazione JMS sono:
• Connection factories e destinazioni: sono oggetti amministrati da JNDI.
– Connection Factories: sono oggetti che il client usa per creare connessioni con il provider che
incapsulano un inseme di parametri per la connessione. Ogni connection factory è un istanza di
QueueConnectionFactory o TopicConnectionFactory. Questi nomi sono anche quelli utilizzati di
default da J2EE. Per creare delle nuove connection factory da J2EE:
j2eeadmin -addJmsFactory MiaQFactory queue
j2eeadmin -addJmsFactory MiaTFactory topic
Quando un client vuole ottenere una connection factory:
Context c t x = new I n i t i a l C o n t e x t ( ) ;
QueueConnectionFactory q u e u e C o n n e c t i o n F a c t o r y =( QueueConnectionFactory ) c t x .
lo oku p ( ” QueueConnectionFactory ” ) ;
T o p i c C o n n e c t i o n F a c t o r y t o p i c C o n n e c t i o n F a c t o r y =( T o p i c C o n n e c t i o n F a c t o r y ) c t x .
lo oku p ( ” T o p i c C o n n e c t i o n F a c t o r y ” ) ;
– Destinazioni: sono oggetti sche il client usa come destinazioni o sorgenti di messaggi. Nel
dominio P2P le destinazioni sono chiamate queue, mentre nel dominio publish/subscribe sono
chiamate topic. Per creare delle nuove destinazioni da J2EE:
j2eeadmin -addJmsDestination MiaQueue queue
j2eeadmin -addJmsDestination MioTopic topic
In JBOSS:
<?xml version=” 1 . 0 ” e n c o d i n g=”UTF−8” ?>
<s e r v e r>
<mbean code=” o r g . j b o s s . mq . s e r v e r . jmx . Queue”
name=” j b o s s . mq . d e s t i n a t i o n : s e r v i c e=Queue , name=myQueue”>
<depends o p t i o n a l −a t t r i b u t e −name=” D e s t i n a t i o n M a n a g e r ”>j b o s s . m q : s e r v i c e=
D e s t i n a t i o n M a n a g e r</ depends>
</mbean>
</ s e r v e r>
Per ottenere delle destinazioni:
Topic myTopic = ( Topic ) c t x . loo kup ( ”MyTopic” ) ;
Queue myQueue = ( Queue ) c t x . loo kup ( ”MyQueue” ) ;
• Connessioni: sono connessioni virtuali con un provider JMS. Per ottenere delle connesioni a partire
da connection factory in precedenza istanziate:
QueueConnection queueConnection =
queueConnectionFactory . createQueueConnection ( ) ;
T o p i cConnection t o p i c C o n n e c t i o n =
topicConnectionFactory . createTopicConnection () ;
Per poter iniziare a consumare messaggi bisogna chiamare il metodo start sulla connessione, il metodo
stop invece disabilita temporaneamente la consegna di messaggi senza chiudere la connessione.Per
chiudere completamente la connessione bisogna invocare il suo metodo close().
• Sessioni: sono contesti nei quali si possono produrre e consumare messaggi. Vengono usate per creare
produttori, consumatori e messaggi.
1
TopicSession t o p i c S e s s i o n = topicConnection . createTopicSession ( false , Session .
AUTO ACKNOWLEDGE) ;
Q u e u e S e s s i o n q u e u e S e s s i o n = queueConnection . c r e a t e Q u e u e S e s s i o n ( true , 0 ) ;
Nel caso della topicSession di cui sopra false indica che la sessione non è transazionale mentre
Session.AUTO ACKNOWLEDGE significa che la sessione notifica automaticamente i messaggi che
sono stati ricevuti con successo.
• Produttori: sono oggetti creati dalla sessione e servono ad inviare messaggi.
QueueSender queueSender = q u e u e S e s s i o n . c r e a t e S e n d e r ( myQueue ) ;
T o p i c P u b l i s h e r t o p i c P u b l i s h e r = t o p i c S e s s i o n . c r e a t e P u b l i s h e r ( myTopic ) ;
I messaggi vengono inviati tramite il metodo send o publish:
queueSender . send ( message ) ;
t o p i c P u b l i s h e r . p u b l i s h ( message ) ;
• Consumatori: sono oggetti creati dalla sessione e servono a ricevere messaggi.
QueueReceiver q u e u e R e c e i v e r = q u e u e S e s s i o n . c r e a t e R e c e i v e r ( myQueue ) ;
T o p i c S u b s c r i b e r t o p i c S u b s c r i b e r =t o p i c S e s s i o n . c r e a t e S u b s c r i b e r ( myTopic ) ;
Una volta creato il consumatore, questo diventa attivo cioè pronto a ricevere messaggi, per renderlo
inattivo bisogna chiamare il metodo close(). La consegnadi messaggi comunque non inizia finchè non
viene chiamato il metodo start sulla connessione.
Si può usare poi il metodo receive() per consumare i messaggi in modalità sincrona (sempre dopo
aver invocato start sulla connessione).
Invece per consumare i messaggi in modalità asincrona si usa un message listener.
– Message listener: è un oggetto che si comporta come un gestore di eventi asincroni. Implementa
l’interfaccia MessageListener che contiene un metodo, onMessage. Nell’implementazione di
tale metodo viene specificato il comportamento alla ricezione del messaggio. Il consumatore si
registra al listener di interesse:
T o p i c L i s t e n e r t o p i c L i s t e n e r = new T o p i c L i s t e n e r ( ) ;
topicSubscriber . setMessageListener ( topicListener ) ;
• Messaggi: Un messaggio è composto da:
– Header: ogni campo dello header ha i metodi set e get. Alcuni campi devono essere settati
esplicitamente ma la maggior parte di essi viene settata automaticamente dai metodi send o
publish (e in questi casi gli eventuali campi già settati manualmente vengono sovrascritti).
– Properties (opzionali)
– Body (opzionale): ci sono 5 tipi divers di body:
∗ TextMessage: messaggio testuale
∗ MapMessage: coppie nome/valore in cui il noe è una stringa ed il valore è un tipo primitivo
di java.
∗ ByteMessage: uno strem di bytes
∗ StreamMessage: uno stream di valori primitivi di java
∗ ObjectMessage: un oggetto di tipo serializzabile
∗ Message: costituito dai soli campi header e properties
2
Per creare un messaggio di tipo testuale:
TextMessage message = q u e u e S e s s i o n . c r e a t e T e x t M e s s a g e ( ) ;
message . s e t T e x t ( m s g t e x t ) ; // m s g t e x t è d i t i p o S t r i n g
queueSender . send ( message ) ;
Quando il messaggio viene consumato esso arriva come messaggio generico (di tipo Message) e quindi
deve essere fatto il cast al tipo appropriato.
Message m = q u e u e R e c e i v e r . r e c e i v e ( ) ;
i f (m instanceof TextMessage ) {
TextMessage message = ( TextMessage ) m;
System . out . p r i n t l n ( ” Reading message : ” + message . g et Te xt ( ) ) ;
}
else {
// G e s t i s c i l ’ e r r o r e
}
1.1
Esempio MDB
Un semplice esempio di Message Driven Bean che utilizza una coda per ricevere messaggi.
Listing 1: FirstMDB.java
import j a v a x . e j b . MessageDrivenBean ;
import j a v a x . e j b . MessageDrivenContext ;
import j a v a x . jms . Message ;
import j a v a x . jms . M e s s a g e L i s t e n e r ;
import j a v a x . jms . TextMessage ;
public c l a s s FirstMDB implements MessageDrivenBean , M e s s a g e L i s t e n e r {
public void e j b C r e a t e ( ) {
System . out . p r i n t l n ( ” −− Creo l ’MDB −− ” ) ;
}
public void s e t M e s s a g e D r i v e n C o n t e x t ( MessageDrivenContext c x t ) {
System . out . p r i n t l n ( ” −− S e t t o i l c o n t e s t o −− ” ) ;
}
public void ejbRemove ( ) {
System . out . p r i n t l n ( ” −− Rimuovo l ’MDB −− ” ) ;
}
public void onMessage ( Message msg ) {
System . out . p r i n t l n ( ” −− R i c e v u t o un m e s s a g g i o −− ” ) ;
S t r i n g t e x t = null ;
i f ( msg instanceof TextMessage ) {
try {
t e x t = ( ( TextMessage ) msg ) . ge tT e xt ( ) ;
System . out . p r i n t l n ( ”
”+t e x t ) ;
} catch ( E x c e p t i o n ex ) {
ex . p r i n t S t a c k T r a c e ( ) ;
}
}
else {
System . out . p r i n t l n ( ”
non è un m e s s a g g i o t e s t u a l e ! ” ) ;
}
}
}
3
Listing 2: MDBClient.java
import j a v a x . naming . ∗ ;
import j a v a x . jms . ∗ ;
public c l a s s MDBClient {
public s t a t i c void main ( S t r i n g a r g [ ] ) {
try {
/∗ S o l u z i o n e 1 : In a l t e r n a t i v a a l j n d i . p r o p e r t i e s ∗/
/∗ j a v a . u t i l . P r o p e r t i e s prop = new j a v a . u t i l . P r o p e r t i e s ( ) ;
prop . p u t ( C o n t e x t . INITIAL CONTEXT FACTORY, ” o r g . j n p . i n t e r f a c e s .
NamingContextFactory ”) ;
prop . p u t ( C o n t e x t . URL PKG PREFIXES, ” o r g . j n p . i n t e r f a c e s ”) ;
prop . p u t ( C o n t e x t .PROVIDER URL, ” l o c a l h o s t ”) ;
C o n t e x t c o n t e x t = new I n i t i a l C o n t e x t ( prop ) ; ∗/
/∗−−−−−−− S o l u z i o n e 1 −−−−−−−−−−−−−−−−−−∗/
/∗ S o l u z i o n e 2 : C o n t e s t o p r e s o d a l f i l e j n d i . p r o p e r t i e s ∗/
Context c o n t e x t = new I n i t i a l C o n t e x t ( ) ;
/∗−−−−−−− S o l u z i o n e 2 −−−−−−−−−−−−−−−−−−∗/
// Look up d e l l a c o n n e c t i o n f a c t o r y ( QueueConnectionFactory è q u e l l a d i
d e f a u l t p e r JBOSS)
QueueConnectionFactory qu eueFacto ry =( QueueConnectionFactory ) c o n t e x t .
lo oku p ( ” QueueConnectionFactory ” ) ;
// Crea una c o n n e s s i o n e con i l JMS p r o v i d e r
QueueConnection queueConnection = qu eueFacto ry . c r e a t e Q u e u e C o n n e c t i o n ( ) ;
// Crea una s e s s i o n e
Q u e u e S e s s i o n s e s s i o n = queueConnection . c r e a t e Q u e u e S e s s i o n ( f a l s e , S e s s i o n
.AUTO ACKNOWLEDGE) ;
// Lookup d e l l a coda d i d e s t i n a z i o n e su c u i p u b b l i c a r e
Queue queue = ( Queue ) c o n t e x t . l ook up ( ” queue / f i r s t Q u e u e ” ) ;
// Crea un p r o d u t t o r e
QueueSender queueSender = s e s s i o n . c r e a t e S e n d e r ( queue ) ;
// Crea un m e s s a g g i o d i t e s t o
TextMessage message = s e s s i o n . c r e a t e T e x t M e s s a g e ( ) ;
message . s e t T e x t ( ” H e l l o World ! ” ) ;
// I n v i a i l m e s s a g g i o
queueSender . send ( message ) ;
// Chiude l a c o n n e s s i o n e
queueConnection . c l o s e ( ) ;
} catch ( E x c e p t i o n e ) {
e . printStackTrace () ;
}
}
}
Listing 3: jndi.properties
j a v a . naming . f a c t o r y . i n i t i a l =o r g . jnp . i n t e r f a c e s . NamingContextFactory
j a v a . naming . p r o v i d e r . u r l=jnp : // l o c a l h o s t : 1 0 9 9
j a v a . naming . f a c t o r y . u r l . pkgs=o r g . j b o s s . naming : o r g . jnp . i n t e r f a c e s
Listing 4: META-INF/ejb-jar.xml
<?xml version=” 1 . 0 ” e n c o d i n g=”UTF−8” ?>
< !DOCTYPE e j b −j a r PUBLIC ”−//Sun Microsystems , I n c . / /DTD E n t e r p r i s e
JavaBeans 2 . 0 / /EN” ” h t t p : // j a v a . sun . com/ dtd / e j b −j a r 2 0 . dtd ”>
<e j b −j a r>
<e n t e r p r i s e −beans>
4
<message−d r i v e n>
<e j b −name>FirstMDB</ e j b −name>
<e j b −c l a s s>FirstMDB</ e j b −c l a s s>
<message−d r i v e n −d e s t i n a t i o n>
<d e s t i n a t i o n −type>
j a v a x . jms . Queue
</ d e s t i n a t i o n −type>
</ message−d r i v e n −d e s t i n a t i o n>
<t r a n s a c t i o n −type>C o n t a i n e r</ t r a n s a c t i o n −type>
</ message−d r i v e n>
</ e n t e r p r i s e −beans>
</ e j b −j a r>
Listing 5: META-INF/jboss.xml
<j b o s s>
<e n t e r p r i s e −beans>
<message−d r i v e n>
<e j b −name>FirstMDB</ e j b −name>
<d e s t i n a t i o n −j n d i −name>
firstQueue
</ d e s t i n a t i o n −j n d i −name>
</ message−d r i v e n>
</ e n t e r p r i s e −beans>
</ j b o s s>
1.2
Esempio Queue
Listing 6: Receiver.java
import j a v a x . jms . ∗ ;
import j a v a x . naming . ∗ ;
import j a v a . u t i l . P r o p e r t i e s ;
c l a s s T e x t L i s t e n e r implements M e s s a g e L i s t e n e r
{
public void onMessage ( Message message )
{
try
{
TextMessage txtMsg=(TextMessage ) message ;
System . out . p r i n t l n ( ” r i c e v u t o : ” +txtMsg . ge tT ex t ( ) ) ;
}
catch ( JMSException e r r o r )
{ System . out . p r i n t l n ( ” h a i r i c e v u t o una e c c e z i o n e : ”+e r r o r . get Message ( ) ) ; }
}
}
public c l a s s R e c e i v e r
{
public s t a t i c void main ( S t r i n g a r g s [ ] ) throws NamingException , JMSException
{
I n i t i a l C o n t e x t c t x = null ;
QueueConnectionFactory c f = null ;
QueueConnection c o n n e c t i o n = null ;
Q u e u e S e s s i o n s e s s i o n = null ;
Queue d e s t i n a t i o n = null ;
QueueReceiver r e c e i v e r = null ;
// TextMessage message = n u l l ;
5
P r o p e r t i e s p r o p e r t i e s = new P r o p e r t i e s ( ) ;
p r o p e r t i e s . put ( Context . INITIAL CONTEXT FACTORY, ” o r g . jnp . i n t e r f a c e s .
NamingContextFactory ” ) ;
p r o p e r t i e s . put ( Context . URL PKG PREFIXES, ” o r g . jnp . i n t e r f a c e s ” ) ;
p r o p e r t i e s . put ( Context . PROVIDER URL, ” l o c a l h o s t ” ) ;
c t x = new I n i t i a l C o n t e x t ( p r o p e r t i e s ) ;
c f = ( QueueConnectionFactory ) c t x . loo kup ( ” C o n n e c t i o n F a c t o r y ” ) ;
d e s t i n a t i o n = ( Queue ) c t x . l ook up ( ” queue /miaCoda” ) ;
connection = c f . createQueueConnection ( ) ;
s e s s i o n = connection . createQueueSession ( false ,
S e s s i o n .AUTO ACKNOWLEDGE) ;
receiver = session . createReceiver ( destination ) ;
r e c e i v e r . s e t M e s s a g e L i s t e n e r (new T e x t L i s t e n e r ( ) ) ;
connection . start () ;
System . out . p r i n t l n ( ”La c o n n e s s i o n e e ’ p a r t i t a , sono i n a t t e s a d i un m e s s a g g i o ”
);
}
}
Listing 7: Sender.java
import j a v a x . jms . ∗ ;
import j a v a x . naming . ∗ ;
import j a v a . u t i l . P r o p e r t i e s ;
public c l a s s Sender
{
public s t a t i c void main ( S t r i n g a r g s [ ] ) throws NamingException , JMSException
{
I n i t i a l C o n t e x t c t x = null ;
QueueConnectionFactory c f = null ;
QueueConnection c o n n e c t i o n = null ;
Q u e u e S e s s i o n s e s s i o n = null ;
Queue d e s t i n a t i o n = null ;
QueueSender s e n d e r = null ;
TextMessage message = null ;
P r o p e r t i e s p r o p e r t i e s = new P r o p e r t i e s ( ) ;
p r o p e r t i e s . put ( Context . INITIAL CONTEXT FACTORY, ” o r g . jnp . i n t e r f a c e s .
NamingContextFactory ” ) ;
p r o p e r t i e s . put ( Context . URL PKG PREFIXES, ” o r g . jnp . i n t e r f a c e s ” ) ;
p r o p e r t i e s . put ( Context . PROVIDER URL, ” l o c a l h o s t ” ) ;
c t x = new I n i t i a l C o n t e x t ( p r o p e r t i e s ) ;
c f = ( QueueConnectionFactory ) c t x . loo kup ( ” C o n n e c t i o n F a c t o r y ” ) ;
d e s t i n a t i o n = ( Queue ) c t x . l ook up ( ” queue /miaCoda” ) ;
connection = c f . createQueueConnection ( ) ;
s e s s i o n = connection . createQueueSession ( false ,
S e s s i o n .AUTO ACKNOWLEDGE) ;
sender = s e s s i o n . createSender ( destination ) ;
message = s e s s i o n . c r e a t e T e x t M e s s a g e ( ) ;
message . s e t T e x t ( a r g s [ 0 ] ) ;
System . out . p r i n t l n ( ” I n v i o d e l m e s s a g g i o d i a l l a r m e i n c o r s o . . \n” ) ;
s e n d e r . send ( message ) ;
6
connection . close () ;
System . out . p r i n t l n ( ” Messagg io d i a l l a r m e i n v i a t o c o r r e t t a m e n t e . ” ) ;
}
}
1.3
Esempio Topic
Listing 8: Publisher.java
import j a v a x . jms . ∗ ;
import j a v a x . naming . ∗ ;
import j a v a . u t i l . P r o p e r t i e s ;
public c l a s s P u b l i s h e r
{
public s t a t i c void main ( S t r i n g a r g s [ ] ) throws NamingException , JMSException
{
I n i t i a l C o n t e x t c t x = null ;
T o p i c C o n n e c t i o n F a c t o r y c f = null ;
T o p i cConnection c o n n e c t i o n = null ;
T o p i c S e s s i o n s e s s i o n = null ;
Topic d e s t i n a t i o n = null ;
T o p i c P u b l i s h e r p u b l i s h e r = null ;
TextMessage message = null ;
P r o p e r t i e s p r o p e r t i e s = new P r o p e r t i e s ( ) ;
p r o p e r t i e s . put ( Context . INITIAL CONTEXT FACTORY, ” o r g . jnp . i n t e r f a c e s .
NamingContextFactory ” ) ;
p r o p e r t i e s . put ( Context . URL PKG PREFIXES, ” o r g . jnp . i n t e r f a c e s ” ) ;
p r o p e r t i e s . put ( Context . PROVIDER URL, ” l o c a l h o s t ” ) ;
c t x = new I n i t i a l C o n t e x t ( p r o p e r t i e s ) ;
c f = ( T o p i c C o n n e c t i o n F a c t o r y ) c t x . lo oku p ( ” C o n n e c t i o n F a c t o r y ” ) ;
d e s t i n a t i o n = ( Topic ) c t x . loo kup ( ” t o p i c / i n c e n d i o ” ) ;
connection = c f . createTopicConnection () ;
s e s s i o n = connection . createTopicSession ( false ,
S e s s i o n .AUTO ACKNOWLEDGE) ;
publisher = session . createPublisher ( destination ) ;
message = s e s s i o n . c r e a t e T e x t M e s s a g e ( ) ;
message . s e t T e x t ( a r g s [ 0 ] ) ;
System . out . p r i n t l n ( ” I n v i o d e l m e s s a g g i o d i a l l a r m e i n c o r s o . . \n” ) ;
p u b l i s h e r . send ( message ) ;
connection . close () ;
System . out . p r i n t l n ( ” Messagg io d i a l l a r m e i n v i a t o c o r r e t t a m e n t e . ” ) ;
}
}
Listing 9: Subscriber.java
import j a v a x . jms . ∗ ;
import j a v a x . naming . ∗ ;
import j a v a . u t i l . P r o p e r t i e s ;
c l a s s T e x t L i s t e n e r implements M e s s a g e L i s t e n e r
7
{
public void onMessage ( Message message )
{
try
{
TextMessage txtMsg=(TextMessage ) message ;
System . out . p r i n t l n ( ” r i c e v u t o : ” +txtMsg . ge tT ex t ( ) ) ;
}
catch ( JMSException e r r o r )
{ System . out . p r i n t l n ( ” h a i r i c e v u t o una e c c e z i o n e : ”+e r r o r . get Message ( ) ) ; }
}
}
public c l a s s S u b s c r i b e r
{
public s t a t i c void main ( S t r i n g a r g s [ ] ) throws NamingException , JMSException
{
I n i t i a l C o n t e x t c t x = null ;
T o p i c C o n n e c t i o n F a c t o r y c f = null ;
T o p i cConnection c o n n e c t i o n = null ;
T o p i c S e s s i o n s e s s i o n = null ;
Topic d e s t i n a t i o n = null ;
T o p i c S u b s c r i b e r s u b s c r i b e r = null ;
// TextMessage message = n u l l ;
P r o p e r t i e s p r o p e r t i e s = new P r o p e r t i e s ( ) ;
p r o p e r t i e s . put ( Context . INITIAL CONTEXT FACTORY, ” o r g . jnp . i n t e r f a c e s .
NamingContextFactory ” ) ;
p r o p e r t i e s . put ( Context . URL PKG PREFIXES, ” o r g . jnp . i n t e r f a c e s ” ) ;
p r o p e r t i e s . put ( Context . PROVIDER URL, ” l o c a l h o s t ” ) ;
c t x = new I n i t i a l C o n t e x t ( p r o p e r t i e s ) ;
c f = ( T o p i c C o n n e c t i o n F a c t o r y ) c t x . lo oku p ( ” C o n n e c t i o n F a c t o r y ” ) ;
d e s t i n a t i o n = ( Topic ) c t x . loo kup ( ” t o p i c / i n c e n d i o ” ) ;
connection = c f . createTopicConnection () ;
s e s s i o n = connection . createTopicSession ( false ,
S e s s i o n .AUTO ACKNOWLEDGE) ;
subscriber = session . createSubscriber ( destination ) ;
s u b s c r i b e r . s e t M e s s a g e L i s t e n e r (new T e x t L i s t e n e r ( ) ) ;
connection . start () ;
System . out . p r i n t l n ( ”La c o n n e s s i o n e e ’ p a r t i t a , sono i n a t t e s a d i un m e s s a g g i o ”
);
}
}
1.4
Esempio Topic Selection
Listing 10: Publisher.java
import j a v a x . jms . ∗ ;
import j a v a x . naming . ∗ ;
import j a v a . u t i l . P r o p e r t i e s ;
public c l a s s P u b l i s h e r
{
8
public s t a t i c void main ( S t r i n g a r g s [ ] ) throws NamingException , JMSException
{
I n i t i a l C o n t e x t c t x = null ;
T o p i c C o n n e c t i o n F a c t o r y c f = null ;
T o p i cConnection c o n n e c t i o n = null ;
T o p i c S e s s i o n s e s s i o n = null ;
Topic d e s t i n a t i o n = null ;
T o p i c P u b l i s h e r p u b l i s h e r = null ;
TextMessage message = null ;
P r o p e r t i e s p r o p e r t i e s = new P r o p e r t i e s ( ) ;
p r o p e r t i e s . put ( Context . INITIAL CONTEXT FACTORY, ” o r g . jnp . i n t e r f a c e s .
NamingContextFactory ” ) ;
p r o p e r t i e s . put ( Context . URL PKG PREFIXES, ” o r g . jnp . i n t e r f a c e s ” ) ;
p r o p e r t i e s . put ( Context . PROVIDER URL, ” l o c a l h o s t ” ) ;
c t x = new I n i t i a l C o n t e x t ( p r o p e r t i e s ) ;
c f = ( T o p i c C o n n e c t i o n F a c t o r y ) c t x . lo oku p ( ” C o n n e c t i o n F a c t o r y ” ) ;
d e s t i n a t i o n = ( Topic ) c t x . loo kup ( ” t o p i c / a l l a r m e ” ) ;
connection = c f . createTopicConnection () ;
s e s s i o n = connection . createTopicSession ( false ,
S e s s i o n .AUTO ACKNOWLEDGE) ;
publisher = session . createPublisher ( destination ) ;
message = s e s s i o n . c r e a t e T e x t M e s s a g e ( ) ;
message . s e t S t r i n g P r o p e r t y ( ” t i p o A l l a r m e ” , a r g s [ 0 ] ) ;
message . s e t T e x t ( a r g s [ 1 ] ) ;
TextMessage txtMsg=(TextMessage ) message ;
System . out . p r i n t l n ( ” I n v i o d e l m e s s a g g i o d i a l l a r m e i n c o r s o . . \n” ) ;
p u b l i s h e r . send ( message ) ;
connection . close () ;
System . out . p r i n t l n ( ” Messagg io d i a l l a r m e i n v i a t o c o r r e t t a m e n t e . ” ) ;
}
}
Listing 11: Subscriber.java
import j a v a x . jms . ∗ ;
import j a v a x . naming . ∗ ;
import j a v a . u t i l . P r o p e r t i e s ;
c l a s s T e x t L i s t e n e r implements M e s s a g e L i s t e n e r
{
public void onMessage ( Message message )
{
try
{
TextMessage txtMsg=(TextMessage ) message ;
System . out . p r i n t l n ( ” r i c e v u t o : ” +txtMsg . ge tT ex t ( ) ) ;
}
catch ( JMSException e r r o r )
{ System . out . p r i n t l n ( ” h a i r i c e v u t o una e c c e z i o n e : ”+e r r o r . get Message ( ) ) ; }
}
}
public c l a s s S u b s c r i b e r
{
9
public s t a t i c void main ( S t r i n g a r g s [ ] ) throws NamingException , JMSException
{
I n i t i a l C o n t e x t c t x = null ;
T o p i c C o n n e c t i o n F a c t o r y c f = null ;
T o p i cConnection c o n n e c t i o n = null ;
T o p i c S e s s i o n s e s s i o n = null ;
Topic d e s t i n a t i o n = null ;
T o p i c S u b s c r i b e r s u b s c r i b e r = null ;
// TextMessage message = n u l l ;
S t r i n g s e l e c t o r = null ;
P r o p e r t i e s p r o p e r t i e s = new P r o p e r t i e s ( ) ;
p r o p e r t i e s . put ( Context . INITIAL CONTEXT FACTORY, ” o r g . jnp . i n t e r f a c e s .
NamingContextFactory ” ) ;
p r o p e r t i e s . put ( Context . URL PKG PREFIXES, ” o r g . jnp . i n t e r f a c e s ” ) ;
p r o p e r t i e s . put ( Context . PROVIDER URL, ” l o c a l h o s t ” ) ;
c t x = new I n i t i a l C o n t e x t ( p r o p e r t i e s ) ;
c f = ( T o p i c C o n n e c t i o n F a c t o r y ) c t x . lo oku p ( ” C o n n e c t i o n F a c t o r y ” ) ;
d e s t i n a t i o n = ( Topic ) c t x . loo kup ( ” t o p i c / a l l a r m e ” ) ;
connection = c f . createTopicConnection () ;
s e s s i o n = connection . createTopicSession ( false ,
S e s s i o n .AUTO ACKNOWLEDGE) ;
// ∗∗∗∗∗∗∗∗∗∗ d i c h i a r o che i n t e n d o u t i l i z z a r e un s e l e t t o r e ∗∗∗∗∗∗∗∗∗∗∗∗
s e l e c t o r = ” tipoAllarme = ’ incendio ’ ” ;
subscriber = session . createSubscriber ( destination , selector , false ) ;
s u b s c r i b e r . s e t M e s s a g e L i s t e n e r (new T e x t L i s t e n e r ( ) ) ;
connection . start () ;
System . out . p r i n t l n ( ”La c o n n e s s i o n e e ’ p a r t i t a , sono i n a t t e s a d i un m e s s a g g i o ”
);
}
}
Listing 12: Subscriber2.java
import j a v a x . jms . ∗ ;
import j a v a x . naming . ∗ ;
import j a v a . u t i l . P r o p e r t i e s ;
c l a s s T e x t L i s t e n e r 2 implements M e s s a g e L i s t e n e r
{
public void onMessage ( Message message )
{
try
{
TextMessage txtMsg=(TextMessage ) message ;
System . out . p r i n t l n ( ” r i c e v u t o : ” +txtMsg . ge tT ex t ( ) ) ;
}
catch ( JMSException e r r o r )
{ System . out . p r i n t l n ( ” h a i r i c e v u t o una e c c e z i o n e : ”+e r r o r . get Message ( ) ) ; }
}
}
public c l a s s S u b s c r i b e r 2
10
{
public s t a t i c void main ( S t r i n g a r g s [ ] ) throws NamingException , JMSException
{
I n i t i a l C o n t e x t c t x = null ;
T o p i c C o n n e c t i o n F a c t o r y c f = null ;
T o p i cConnection c o n n e c t i o n = null ;
T o p i c S e s s i o n s e s s i o n = null ;
Topic d e s t i n a t i o n = null ;
T o p i c S u b s c r i b e r s u b s c r i b e r = null ;
// TextMessage message = n u l l ;
S t r i n g s e l e c t o r = null ;
P r o p e r t i e s p r o p e r t i e s = new P r o p e r t i e s ( ) ;
p r o p e r t i e s . put ( Context . INITIAL CONTEXT FACTORY, ” o r g . jnp . i n t e r f a c e s .
NamingContextFactory ” ) ;
p r o p e r t i e s . put ( Context . URL PKG PREFIXES, ” o r g . jnp . i n t e r f a c e s ” ) ;
p r o p e r t i e s . put ( Context . PROVIDER URL, ” l o c a l h o s t ” ) ;
c t x = new I n i t i a l C o n t e x t ( p r o p e r t i e s ) ;
c f = ( T o p i c C o n n e c t i o n F a c t o r y ) c t x . lo oku p ( ” C o n n e c t i o n F a c t o r y ” ) ;
d e s t i n a t i o n = ( Topic ) c t x . loo kup ( ” t o p i c / a l l a r m e ” ) ;
connection = c f . createTopicConnection () ;
s e s s i o n = connection . createTopicSession ( false ,
S e s s i o n .AUTO ACKNOWLEDGE) ;
// ∗∗∗∗∗∗∗∗∗∗ d i c h i a r o che i n t e n d o u t i l i z z a r e un s e l e t t o r e ∗∗∗∗∗∗∗∗∗∗∗∗
s e l e c t o r = ” tipoAllarme = ’ rapina ’ ” ;
subscriber = session . createSubscriber ( destination , selector , false ) ;
s u b s c r i b e r . s e t M e s s a g e L i s t e n e r (new T e x t L i s t e n e r 2 ( ) ) ;
connection . start () ;
System . out . p r i n t l n ( ”La c o n n e s s i o n e e ’ p a r t i t a , sono i n a t t e s a d i un m e s s a g g i o ”
);
}
}
1.5
Esempio Queue con lettura sincrona
Listing 13: ReceiverSync.java
import j a v a x . jms . ∗ ;
import j a v a x . naming . ∗ ;
import j a v a . u t i l . P r o p e r t i e s ;
public c l a s s R e c e i v e r S y n c
{
public s t a t i c void main ( S t r i n g a r g s [ ] ) throws NamingException , JMSException
{
I n i t i a l C o n t e x t c t x = null ;
QueueConnectionFactory c f = null ;
QueueConnection c o n n e c t i o n = null ;
Q u e u e S e s s i o n s e s s i o n = null ;
Queue d e s t i n a t i o n = null ;
QueueReceiver r e c e i v e r = null ;
11
TextMessage message = null ;
P r o p e r t i e s p r o p e r t i e s = new P r o p e r t i e s ( ) ;
p r o p e r t i e s . put ( Context . INITIAL CONTEXT FACTORY, ” o r g . jnp . i n t e r f a c e s .
NamingContextFactory ” ) ;
p r o p e r t i e s . put ( Context . URL PKG PREFIXES, ” o r g . jnp . i n t e r f a c e s ” ) ;
p r o p e r t i e s . put ( Context . PROVIDER URL, ” l o c a l h o s t ” ) ;
c t x = new I n i t i a l C o n t e x t ( p r o p e r t i e s ) ;
c f = ( QueueConnectionFactory ) c t x . loo kup ( ” C o n n e c t i o n F a c t o r y ” ) ;
d e s t i n a t i o n = ( Queue ) c t x . l ook up ( ” queue /miaCoda” ) ;
connection = c f . createQueueConnection ( ) ;
s e s s i o n = connection . createQueueSession ( false ,
S e s s i o n .AUTO ACKNOWLEDGE) ;
receiver = session . createReceiver ( destination ) ;
connection . start () ;
do
{
message=(TextMessage ) r e c e i v e r . r e c e i v e ( ) ;
System . out . p r i n t l n ( ” r i c e v u t o m e s s a g g i o : ”+message . g et Te xt ( ) ) ;
}
while ( ! message . g et Te xt ( ) . e q u a l s ( ” s t o p ” ) ) ;
}
}
Listing 14: SenderSync.java
import j a v a x . jms . ∗ ;
import j a v a x . naming . ∗ ;
import j a v a . u t i l . P r o p e r t i e s ;
public c l a s s SenderSync
{
public s t a t i c void main ( S t r i n g a r g s [ ] ) throws NamingException , JMSException
{
I n i t i a l C o n t e x t c t x = null ;
QueueConnectionFactory c f = null ;
QueueConnection c o n n e c t i o n = null ;
Q u e u e S e s s i o n s e s s i o n = null ;
Queue d e s t i n a t i o n = null ;
QueueSender s e n d e r = null ;
TextMessage message = null ;
P r o p e r t i e s p r o p e r t i e s = new P r o p e r t i e s ( ) ;
p r o p e r t i e s . put ( Context . INITIAL CONTEXT FACTORY, ” o r g . jnp . i n t e r f a c e s .
NamingContextFactory ” ) ;
p r o p e r t i e s . put ( Context . URL PKG PREFIXES, ” o r g . jnp . i n t e r f a c e s ” ) ;
p r o p e r t i e s . put ( Context . PROVIDER URL, ” l o c a l h o s t ” ) ;
c t x = new I n i t i a l C o n t e x t ( p r o p e r t i e s ) ;
c f = ( QueueConnectionFactory ) c t x . loo kup ( ” C o n n e c t i o n F a c t o r y ” ) ;
d e s t i n a t i o n = ( Queue ) c t x . l ook up ( ” queue /miaCoda” ) ;
connection = c f . createQueueConnection ( ) ;
s e s s i o n = connection . createQueueSession ( false ,
S e s s i o n .AUTO ACKNOWLEDGE) ;
12
sender = s e s s i o n . createSender ( destination ) ;
message = s e s s i o n . c r e a t e T e x t M e s s a g e ( ) ;
message . s e t T e x t ( a r g s [ 0 ] ) ;
System . out . p r i n t l n ( ” I n v i o d e l m e s s a g g i o d i a l l a r m e i n c o r s o . . \n” ) ;
s e n d e r . send ( message ) ;
connection . close () ;
System . out . p r i n t l n ( ” Messagg io d i a l l a r m e i n v i a t o c o r r e t t a m e n t e . ” ) ;
}
}
13