KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > space > TransactedSpaceMessageReceiver


1 /*
2  * $Id: TransactedSpaceMessageReceiver.java 3865 2006-11-09 17:11:08Z Lajos $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers.space;
12
13 import org.apache.commons.collections.MapUtils;
14 import org.mule.config.i18n.Message;
15 import org.mule.impl.MuleMessage;
16 import org.mule.providers.ConnectException;
17 import org.mule.providers.TransactedPollingMessageReceiver;
18 import org.mule.umo.UMOComponent;
19 import org.mule.umo.endpoint.UMOEndpoint;
20 import org.mule.umo.lifecycle.InitialisationException;
21 import org.mule.umo.provider.UMOConnector;
22 import org.mule.umo.provider.UMOMessageAdapter;
23 import org.mule.umo.space.UMOSpace;
24 import org.mule.umo.space.UMOSpaceException;
25
26 import java.util.List JavaDoc;
27 import java.util.Properties JavaDoc;
28
29 /**
30  * Registers a transacted message listener on a Space.
31  */

32 public class TransactedSpaceMessageReceiver extends TransactedPollingMessageReceiver
33 {
34     private UMOSpace space;
35     private SpaceConnector connector;
36
37     public TransactedSpaceMessageReceiver(UMOConnector connector,
38                                           UMOComponent component,
39                                           final UMOEndpoint endpoint) throws InitialisationException
40     {
41         super(connector, component, endpoint, new Long JavaDoc(0));
42         this.connector = (SpaceConnector)connector;
43         this.frequency = MapUtils.getLongValue(endpoint.getProperties(), "frequency", 100000L);
44     }
45
46     protected List JavaDoc getMessages() throws Exception JavaDoc
47     {
48         Object JavaDoc message = space.take(frequency);
49         if (message == null)
50         {
51             return null;
52         }
53
54         // Process message
55
if (logger.isDebugEnabled())
56         {
57             logger.debug("Message received it is of type: " + message.getClass().getName());
58         }
59
60         UMOMessageAdapter adapter = connector.getMessageAdapter(message);
61         routeMessage(new MuleMessage(adapter), true);
62         return null;
63     }
64
65     protected void processMessage(Object JavaDoc message) throws Exception JavaDoc
66     {
67         // This method is never called as the message is processed when received
68
}
69
70     public void doConnect() throws Exception JavaDoc
71     {
72         String JavaDoc destination = endpoint.getEndpointURI().getAddress();
73
74         Properties JavaDoc props = new Properties JavaDoc();
75         props.putAll(endpoint.getProperties());
76         try
77         {
78             logger.info("Connecting to space: " + destination);
79             space = connector.getSpace(endpoint);
80         }
81         catch (UMOSpaceException e)
82         {
83             throw new ConnectException(new Message("space", 1, destination), e, this);
84         }
85     }
86
87     public void doDisconnect() throws Exception JavaDoc
88     {
89         // template method
90
}
91
92     public UMOSpace getSpace()
93     {
94         return space;
95     }
96 }
97
Popular Tags