Skip to content

Commit d862974

Browse files
author
Dariusz Suchojad
committed
SESPRINGPYTHONPY-12: Merged branch into trunk. Verified all tests pass (including jms_test_cases). Generated HTML documentation and all looks fine.
git-svn-id: https://src.springframework.org/svn/se-springpython-py/trunk@713 ce8fead1-4192-4296-8608-a705134b927f
2 parents 3a5b4c2 + 3fe954b commit d862974

File tree

11 files changed

+4012
-13
lines changed

11 files changed

+4012
-13
lines changed
Loading

springpython/docs/reference/src/index.xml

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
<!ENTITY aop SYSTEM "aop.xml">
77
<!ENTITY transaction SYSTEM "transaction.xml">
88
<!ENTITY dao SYSTEM "dao.xml">
9-
<!ENTITY remoting SYSTEM "remoting.xml">
10-
<!ENTITY security SYSTEM "security.xml">
9+
<!ENTITY remoting SYSTEM "remoting.xml">
10+
<!ENTITY security SYSTEM "security.xml">
1111
<!ENTITY plugins SYSTEM "plugins.xml">
12-
<!ENTITY samples SYSTEM "samples.xml">
12+
<!ENTITY jms SYSTEM "jms.xml">
13+
<!ENTITY samples SYSTEM "samples.xml">
1314
]>
1415
<book>
1516
<bookinfo>
@@ -36,15 +37,16 @@
3637
<!-- front matter -->
3738
<toc/>
3839

39-
&preface;
40+
&preface;
4041
&overview;
41-
&objects;
42-
&aop;
43-
&dao;
44-
&transaction;
45-
&security;
42+
&objects;
43+
&aop;
44+
&dao;
45+
&transaction;
46+
&security;
4647
&remoting;
48+
&jms;
4749
&plugins;
48-
&samples;
50+
&samples;
4951

5052
</book>

springpython/docs/reference/src/jms.xml

Lines changed: 1625 additions & 0 deletions
Large diffs are not rendered by default.

springpython/docs/reference/src/overview.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@
6565
server pieces using the IoC container, then going from local to distributed is
6666
just a configuration change.</para>
6767
</listitem>
68+
69+
<listitem>
70+
<para><link linkend="jms">JMS Messaging</link> - Connect to Java
71+
or Python applications using queueing middleware. Spring Python
72+
can act as a standalone client of a JMS provider with no
73+
Java EE infrastructure needed on Python side.</para>
74+
</listitem>
75+
6876
<listitem>
6977
<para><link linkend="plugins">Plug-ins/command-line tool</link> - Use the plugin
7078
system designed to help you rapidly develop applications.</para>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
Copyright 2006-2008 SpringSource (http://springsource.com), All Rights Reserved
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
"""
16+
17+
# JMS constants
18+
19+
DELIVERY_MODE_NON_PERSISTENT = 1
20+
DELIVERY_MODE_PERSISTENT = 2
21+
22+
RECEIVE_TIMEOUT_INDEFINITE_WAIT = 0
23+
RECEIVE_TIMEOUT_NO_WAIT = -1
24+
25+
DEFAULT_DELIVERY_MODE = DELIVERY_MODE_PERSISTENT
26+
DEFAULT_TIME_TO_LIVE = 0
27+
28+
class JMSException(Exception):
29+
""" Base class for all JMS-related exceptions.
30+
"""
31+
32+
class NoMessageAvailableException(JMSException):
33+
""" Raised when the jms_template's call to receive returned no message
34+
in the expected wait interval.
35+
"""
36+
37+
class WebSphereMQJMSException(JMSException):
38+
""" Class for exceptions related to WebSphereMQ only.
39+
"""
40+
def __init__(self, message=None, completion_code=None, reason_code=None):
41+
JMSException.__init__(self, message)
42+
self.message = message
43+
self.completion_code = completion_code
44+
self.reason_code = reason_code
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
"""
2+
Copyright 2006-2008 SpringSource (http://springsource.com), All Rights Reserved
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
"""
16+
17+
# stdlib
18+
import locale
19+
from string import Template
20+
from cStringIO import StringIO
21+
22+
# Spring Python
23+
from springpython.jms import JMSException
24+
from springpython.jms import DEFAULT_DELIVERY_MODE
25+
26+
__all__ = ["MessageConverter", "JmsTemplate", "TextMessage"]
27+
28+
# These attributes have special meaning to connection factories.
29+
reserved_attributes = set(("text", "jms_correlation_id", "jms_delivery_mode",
30+
"jms_destination", "jms_expiration", "jms_message_id", "jms_priority",
31+
"jms_redelivered", "jms_reply_to", "jms_timestamp", "max_chars_printed",
32+
"JMS_IBM_Report_Exception", "JMS_IBM_Report_Expiration", "JMS_IBM_Report_COA",
33+
"JMS_IBM_Report_COD", "JMS_IBM_Report_PAN", "JMS_IBM_Report_NAN",
34+
"JMS_IBM_Report_Pass_Msg_ID", "JMS_IBM_Report_Pass_Correl_ID",
35+
"JMS_IBM_Report_Discard_Msg", "JMSXGroupID", "JMSXGroupSeq", "JMS_IBM_Feedback",
36+
"JMS_IBM_Last_Msg_In_Group", "JMSXUserID", "JMS_IBM_PutTime", "JMS_IBM_PutDate",
37+
"JMSXAppID"))
38+
39+
# Magic methods are also forbidden.
40+
reserved_attributes.update(set(dir(object) + ["__weakref__", "__dict__", "__module__"]))
41+
42+
43+
text_message_template = """
44+
JMS message class: jms_text
45+
jms_delivery_mode: $jms_delivery_mode
46+
jms_expiration: $jms_expiration
47+
jms_priority: $jms_priority
48+
jms_message_id: $jms_message_id
49+
jms_timestamp: $jms_timestamp
50+
jms_correlation_id: $jms_correlation_id
51+
jms_destination: $jms_destination
52+
jms_reply_to: $jms_reply_to
53+
jms_redelivered: $jms_redelivered
54+
""".lstrip()
55+
56+
class MessageConverter(object):
57+
def to_message(self, object_to_be_converted_to_a_message):
58+
raise NotImplementedError("Should be implemented by subclasses")
59+
60+
def from_message(self, message_to_be_converted_to_an_object):
61+
raise NotImplementedError("Should be implemented by subclasses")
62+
63+
class JmsTemplate(object):
64+
def __init__(self, factory=None, delivery_persistent=None,
65+
priority=None, time_to_live=None, message_converter=None,
66+
default_destination=None):
67+
68+
self.factory = factory
69+
70+
# QoS
71+
self.delivery_persistent = delivery_persistent
72+
self.priority = priority
73+
self.time_to_live = time_to_live
74+
75+
self.message_converter = message_converter
76+
self.default_destination = default_destination
77+
78+
def convert_and_send(self, object_, destination=None):
79+
if not self.message_converter:
80+
raise JMSException("Couldn't send the message, no message converter set")
81+
82+
self.send(self.message_converter.to_message(object_), destination)
83+
84+
def send(self, message, destination=None):
85+
if isinstance(message, basestring):
86+
message = TextMessage(message)
87+
88+
if destination:
89+
dest = destination
90+
elif self.default_destination:
91+
dest = self.default_destination
92+
else:
93+
raise JMSException("No destination given and no default destination set")
94+
95+
message.jms_destination = dest
96+
97+
self.factory.send(message, dest)
98+
99+
def receive(self, destination=None, timeout=1000):
100+
101+
if destination:
102+
dest = destination
103+
elif self.default_destination:
104+
dest = self.default_destination
105+
else:
106+
raise JMSException("No destination given and no default destination set")
107+
108+
return self.factory.receive(dest, timeout)
109+
110+
def receive_and_convert(self, destination=None, timeout=1000):
111+
if not self.message_converter:
112+
raise JMSException("Couldn't receive a message, no message converter set")
113+
114+
return self.message_converter.from_message(self.receive(destination, timeout))
115+
116+
def open_dynamic_queue(self):
117+
return self.factory.open_dynamic_queue()
118+
119+
def close_dynamic_queue(self, dynamic_queue_name):
120+
self.factory.close_dynamic_queue(dynamic_queue_name)
121+
122+
class TextMessage(object):
123+
def __init__(self, text=None, jms_correlation_id=None, jms_delivery_mode=None,
124+
jms_destination=None, jms_expiration=None, jms_message_id=None,
125+
jms_priority=None, jms_redelivered=None, jms_reply_to=None,
126+
jms_timestamp=None, max_chars_printed=100):
127+
128+
self.text = text
129+
self.jms_correlation_id = jms_correlation_id
130+
131+
self.jms_delivery_mode = jms_delivery_mode or DEFAULT_DELIVERY_MODE
132+
133+
self.jms_destination = jms_destination
134+
self.jms_expiration = jms_expiration
135+
self.jms_message_id = jms_message_id
136+
self.jms_priority = jms_priority
137+
self.jms_redelivered = jms_redelivered
138+
self.jms_reply_to = jms_reply_to
139+
self.jms_timestamp = jms_timestamp
140+
self.max_chars_printed = max_chars_printed
141+
142+
def __str__(self):
143+
basic_data = {
144+
"jms_delivery_mode": self.jms_delivery_mode,
145+
"jms_expiration":self.jms_expiration,
146+
"jms_priority":self.jms_priority,
147+
"jms_message_id":self.jms_message_id,
148+
"jms_timestamp":self.jms_timestamp,
149+
"jms_correlation_id":self.jms_correlation_id,
150+
"jms_destination":self.jms_destination,
151+
"jms_reply_to":self.jms_reply_to,
152+
"jms_redelivered":self.jms_redelivered,
153+
}
154+
155+
buff = StringIO()
156+
buff.write(Template(text_message_template).safe_substitute(basic_data))
157+
158+
user_attrs = set(dir(self)) - reserved_attributes
159+
user_attrs = list(user_attrs)
160+
user_attrs.sort()
161+
162+
if user_attrs:
163+
for user_attr in user_attrs:
164+
user_attr_value = getattr(self, user_attr)
165+
166+
if isinstance(user_attr_value, unicode):
167+
user_attr_value = user_attr_value.encode("utf-8")
168+
169+
buff.write(" %s:%s\n" % (user_attr, user_attr_value))
170+
171+
if self.text != None:
172+
text_to_show = self.text[:self.max_chars_printed]
173+
174+
if isinstance(text_to_show, unicode):
175+
text_to_show = text_to_show.encode("utf-8")
176+
177+
buff.write(text_to_show)
178+
179+
if len(text_to_show) < len(self.text):
180+
omitted = locale.format("%d", (len(self.text) - len(text_to_show)), True)
181+
buff.write("\nAnother ")
182+
buff.write(omitted)
183+
buff.write(" character(s) omitted")
184+
else:
185+
buff.write("<None>")
186+
187+
value = buff.getvalue()
188+
buff.close()
189+
190+
return value

0 commit comments

Comments
 (0)