JMS
一、初始化











二、根據(jù)名稱得到隊列連接工廠 隊列




三、根據(jù)隊列連接工廠和隊列創(chuàng)建連接 再用連接創(chuàng)建session




四、根據(jù)session創(chuàng)建發(fā)送者、接收者、消息









五、發(fā)送消息 接收消息




六、完整的消息收發(fā)程序



























































































































1
package jmssample;
2
3
import java.util.Hashtable;
4
import javax.jms.*;
5
import javax.naming.Context;
6
import javax.naming.InitialContext;
7
import javax.naming.NamingException;
8
9
/**
10
* This example shows how to establish a connection to
11
* and receive messages from a JMS queue. The classes in this
12
* package operate on the same JMS queue. Run the classes together to
13
* witness messages being sent and received, and to browse the queue
14
* for messages. This class is used to receive and remove messages
15
* from the queue.
16
*
17
* @author Copyright (c) 1999-2003 by BEA Systems, Inc. All Rights Reserved.
18
*/
19
public class QueueReceive implements MessageListener
20
{
21
// Defines the JNDI context factory.
22
public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
23
24
// Defines the JNDI provider url.
25
public final static String PROVIDER_URL=" t3://localhost:7001";
26
27
// Defines the JMS connection factory for the queue.
28
public final static String JMS_FACTORY="SendJMSFactory";
29
30
// Defines the queue.
31
public final static String QUEUE="SendJMSQueue";
32
33
private QueueConnectionFactory qconFactory;
34
private QueueConnection qcon;
35
private QueueSession qsession;
36
private QueueReceiver qreceiver;
37
private Queue queue;
38
private boolean quit = false;
39
40
/**
41
* Message listener interface.
42
* @param msg message
43
*/
44
public void onMessage(Message msg)
45
{
46
try {
47
String msgText;
48
if (msg instanceof TextMessage) {
49
msgText = ((TextMessage)msg).getText();
50
} else {
51
msgText = msg.toString();
52
}
53
54
System.out.println("Message Received: "+ msgText );
55
56
if (msgText.equalsIgnoreCase("quit")) {
57
synchronized(this) {
58
quit = true;
59
this.notifyAll(); // Notify main thread to quit
60
}
61
}
62
} catch (JMSException jmse) {
63
jmse.printStackTrace();
64
}
65
}
66
67
/**
68
* Creates all the necessary objects for receiving
69
* messages from a JMS queue.
70
*
71
* @param ctx JNDI initial context
72
* @param queueName name of queue
73
* @exception NamingException if operation cannot be performed
74
* @exception JMSException if JMS fails to initialize due to internal error
75
*/
76
public void init(Context ctx, String queueName)
77
throws NamingException, JMSException
78
{
79
qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
80
qcon = qconFactory.createQueueConnection();
81
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
82
queue = (Queue) ctx.lookup(queueName);
83
qreceiver = qsession.createReceiver(queue);
84
qreceiver.setMessageListener(this);
85
qcon.start();
86
}
87
88
/**
89
* Closes JMS objects.
90
* @exception JMSException if JMS fails to close objects due to internal error
91
*/
92
public void close()throws JMSException
93
{
94
qreceiver.close();
95
qsession.close();
96
qcon.close();
97
}
98
/**
99
* main() method.
100
*
101
* @param args WebLogic Server URL
102
* @exception Exception if execution fails
103
*/
104
105
public static void main(String[] args) throws Exception {
106
107
InitialContext ic = getInitialContext();
108
QueueReceive qr = new QueueReceive();
109
qr.init(ic, QUEUE);
110
111
System.out.println("JMS Ready To Receive Messages (To quit, send a \"quit\" message).");
112
113
// Wait until a "quit" message has been received.
114
synchronized(qr) {
115
while (! qr.quit) {
116
try {
117
qr.wait();
118
} catch (InterruptedException ie) {}
119
}
120
}
121
qr.close();
122
}
123
124
private static InitialContext getInitialContext()
125
throws NamingException
126
{
127
Hashtable env = new Hashtable();
128
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
129
env.put(Context.PROVIDER_URL, PROVIDER_URL);
130
return new InitialContext(env);
131
}
132
133
134
}
135

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

127

128

129

130

131

132

133

134

135

posted on 2008-02-18 11:35 曉宇 閱讀(371) 評論(0) 編輯 收藏 所屬分類: J2EE