2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.slc
.jms
;
21 import javax
.jms
.DeliveryMode
;
22 import javax
.jms
.Destination
;
23 import javax
.jms
.JMSException
;
24 import javax
.jms
.Message
;
26 import org
.apache
.commons
.logging
.Log
;
27 import org
.apache
.commons
.logging
.LogFactory
;
28 import org
.argeo
.slc
.SlcException
;
29 import org
.argeo
.slc
.msg
.event
.SlcEvent
;
30 import org
.argeo
.slc
.msg
.event
.SlcEventPublisher
;
31 import org
.springframework
.jms
.core
.JmsTemplate
;
32 import org
.springframework
.jms
.core
.MessagePostProcessor
;
34 public class JmsSlcEventPublisher
implements SlcEventPublisher
{
35 private static final Log log
= LogFactory
36 .getLog(JmsSlcEventPublisher
.class);
37 private Destination eventsDestination
;
38 private JmsTemplate jmsTemplate
;
40 public void publish(final SlcEvent event
) {
41 if (jmsTemplate
.getDeliveryMode() != DeliveryMode
.PERSISTENT
)
42 throw new SlcException(
43 "Delivery mode has to be persistent in order to have durable subscription");
45 jmsTemplate
.convertAndSend(eventsDestination
, event
,
46 new MessagePostProcessor() {
48 public Message
postProcessMessage(Message message
)
50 Map
<String
, String
> headers
= event
.getHeaders();
51 for (String key
: headers
.keySet()) {
52 message
.setStringProperty(key
, headers
.get(key
));
57 if (log
.isTraceEnabled()) {
58 log
.trace("Event " + event
.toString() + " sent to "
59 + eventsDestination
.toString());
64 public void setEventsDestination(Destination eventsDestination
) {
65 this.eventsDestination
= eventsDestination
;
68 public void setJmsTemplate(JmsTemplate jmsTemplate
) {
69 this.jmsTemplate
= jmsTemplate
;