Java消息服务(JMS)学习小结-2

上一篇 / 下一篇  2012-07-13 15:30:48 / 个人分类:Java

e$i8g)`8qx0  消息生产者程序如下51Testing软件测试网vTt*d@?7Mo4d

)|l&p!]tP)wv M5kF7Z/p051Testing软件测试网*U+P ^-gH U"z

package org.jms.test;51Testing软件测试网SbB8n$J/a"_gB]
import java.io.*;
j$y:{'L3` m0mport javax.jms.*;
k+k}$EQF F0import javax.naming.*;
$vd'Mmb&V)x0public class Sender {51Testing软件测试网;kB1J;`)l B
    public static void main(String[] args) {51Testing软件测试网1@B2W|(ZL(nG
        new Sender().send();
n:DB.AX*O-{/C#Ch0    }
0Ub cP,R0    public void send() {51Testing软件测试网*[*N ? b o'u
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));51Testing软件测试网HS } X#XmZ9A$d
        try {51Testing软件测试网mCaRow?&hX0`
            //Prompt for JNDI names51Testing软件测试网5[v7\n v)M6]fW!u
            System.out.println("Enter ConnectionFactory name:");
E!T'Q^*K0            String factoryName = reader.readLine();
:A*m,w9B0bWp-E Jgg0            System.out.println("Enter Destination name:");51Testing软件测试网C*D O-?{C
            String destinationName = reader.readLine();51Testing软件测试网Afz2hT.lM
            //Look up administered objects51Testing软件测试网2a5V6xu lH'e"j V A
            InitialContext initContext = new InitialContext();
g:l$];[eh^gR5D0            ConnectionFactory factory =51Testing软件测试网TO$Iq/Y ^&s'h%S-j?
                (ConnectionFactory) initContext.lookup(factoryName);51Testing软件测试网 WKXo3u/\'~ H$x
            Destination destination = (Destination) initContext.lookup(destinationName);
sIh }C'| }*kVKw0            initContext.close();51Testing软件测试网_-n N rg
            //Create JMS objects51Testing软件测试网*o1hdn6M5Vbs[s
            Connection connection = factory.createConnection();
l1a+EwJRC;`7| g0            Session session =
/lZNH I0a0                connection.createSession(false, Session.AUTO_ACKNOWLEDGE);51Testing软件测试网 d/ud#N"fI7g
            MessageProducer sender = session.createProducer(queue);
Rx7YG4}[#Q0            //Send messages51Testing软件测试网_Gv)h h`D2lE
            String messageText = null;
``@ o3o%J UJ3~0            while (true) {51Testing软件测试网;|?v4z Wxe.f
                System.out.println("Enter message to send or 'quit':");
'db:j4B0V0                messageText = reader.readLine();
3C|ZZ_u%D0                if ("quit".equals(messageText))
m"WOGQGg0                    break;
b.`^q9D Tj0                TextMessage message = session.createTextMessage(messageText);51Testing软件测试网+erejR6j ?N
                sender.send(message);51Testing软件测试网6i8u+qg|/G.@~
            }
:g,Vl`[L z0            //Exit51Testing软件测试网\#q0@$KEpnjX
            System.out.println("Exiting...");
"L0v:i(C7E)O0            reader.close();51Testing软件测试网$O$]Ggr3E
            connection.close();
1R:Hb1C-[dV#t0            System.out.println("Goodbye!");
v5l"x|1r0        } catch (Exception e) {
$ij5nm/Bd9Y&^"W0            e.printStackTrace();51Testing软件测试网vd(U#PH%k4L
            System.exit(1);51Testing软件测试网3F _(R*]"D-]
        }51Testing软件测试网!vh a _#hn
    }
p @,HOk)O0}51Testing软件测试网?J^]nu
 消息消费者程序如下

I-Bh^M6E;u6F7_0

YKI z d O,U:y5i2w0package compute;51Testing软件测试网+A6{*^HXMY
import java.io.*;
ZiC#Y+clT4?Af~0import javax.jms.*;
!_ Jw3ZS9Es0import javax.naming.*;51Testing软件测试网ip)^ k0h:WC
public class Receiver implements MessageListener {51Testing软件测试网(h#@*vv:U We3B
    private boolean stop = false;
9D4~.In;nT+Ns!o5\+i0    public static void main(String[] args) {
({`jWM,x@0@1dF0        new Receiver().receive();
,i1K!} S)^0    }51Testing软件测试网h-vU'\]2p1p1|
    public void receive() {
9A`6e;Wpb"G-s~F/e4g0        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
/R;A @8bHT mL`p0        try {51Testing软件测试网3W|Wy!j UJAO
            //Prompt for JNDI names51Testing软件测试网3dx%r%V`-x7q1s4]
            System.out.println("Enter ConnectionFactory name:");
2N;\dR*hI)j9sV0            String factoryName = reader.readLine();51Testing软件测试网 r \?"t$fPE
            System.out.println("Enter Destination name:");
$qQWGmjjb0            String destinationName = reader.readLine();
9|P&[wW$rB0            reader.close();
,IV3T/^H:\0            //Look up administered objects
/k-KO~ JHy1Y0            InitialContext initContext = new InitialContext();
$Yh&]"v4]B?m0            ConnectionFactory factory =
UZ(od$q0                (ConnectionFactory) initContext.lookup(factoryName);51Testing软件测试网+e%`2g{0e1|~FC8Kn
            Destination destination = (Destination) initContext.lookup(destinationName);51Testing软件测试网Y8x4df@'D$e
            initContext.close();51Testing软件测试网]S6a9T`;g
            //Create JMS objects51Testing软件测试网;Q8\;p&[(@m'{
            Connection connection = factory.createConnection();51Testing软件测试网}a)N2a}1];\Z
            Session session =
X4q5DS \{\0                connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
}{4}{BDNN2Y{0            MessageConsumer receiver = session.createConsumer(queue);
!l7v kaT0            receiver.setMessageListener(this);51Testing软件测试网&KYX:T#mL8i
            connection.start();
Y2?#D _T9b&P J0            //Wait for stop
w)Kj;v#x0J.j+A`0            while (!stop) {51Testing软件测试网7pjr Q%YT9} A
                Thread.sleep(1000);
[.ic(Ai&soe0            }
5C$q+k4J xYO5d0            //Exit
hZU0f%g0            System.out.println("Exiting...");51Testing软件测试网 UpDl Y'c-_
            connection.close();51Testing软件测试网9T'k!ij4y*n?-?@
            System.out.println("Goodbye!");
|q9P$\K"h0        } catch (Exception e) {
UD_h @e4Lxh0            e.printStackTrace();
.W(k4K]"f;e0N\n ~0            System.exit(1);
B7X O ?-Sxh0        }51Testing软件测试网 {)f'y4uMmp$[0f
    }
c w3TNz0{0    public void onMessage(Message message) {51Testing软件测试网7vB"L KH,_.E i
        try {51Testing软件测试网ox(QA5Xm
            String msgText = ((TextMessage) message).getText();51Testing软件测试网2I9i8go MkfB"LZ
            System.out.println(msgText);
}[9u5s+F2]0            if ("stop".equals(msgText))51Testing软件测试网[ r%? z"s#e
                stop = true;
5mv/P~`'T0        } catch (JMSException e) {
7z}#DE8Y m$g0            e.printStackTrace();
1GC|%].a ayFd0            stop = true;
U/BK"e5][-[0        }
#bEA9k#S:|Cg0    }
3I!h)n&[*VIh0}51Testing软件测试网4g#q,FT'SJ)f

TAG:

 

评分:0

我来说两句

Open Toolbar