...
The code below is a simple example of how to use the Messaging Client. The JmsMessagingClient constructor includes three required parameters and two optional parameters. The required parameters include the client ID, the MessagingListener instance, and the connection properties mentioned above. The optional parameters include a message selector and flag which determines whether durable subscribers should be used to listen over the topics listed in the properties. More information about each of the available parameters can be found in the JmsMessagingClient javadocs.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
public class Example implements MessagingListener { MessagingClient messagingClient; public void start() throws MessagingException { Properties properties = new Properties(); properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.jndi.ActiveMQInitialContextFactory"); properties.setProperty(Context.PROVIDER_URL, "tcp://localhost:61616"); properties.setProperty(JMSManager.CONNECTION_FACTORY_NAME, "ConnectionFactory"); properties.setProperty("topic.fedora", "fedora.apim.*"); messagingClient = new JmsMessagingClient("example1", this, properties, false); messagingClient.start(); } public void stop() throws MessagingException { messagingClient.stop(false); } public void onMessage(String clientId, Message message) { String messageText = ""; try { messageText = ((TextMessage)message).getText(); } catch(JMSException e) { System.err.println("Error retrieving message text " + e.getMessage()); } System.out.println("Message received: " + messageText + " from client " + clientId); } } |
...
The content of messages sent by Fedora takes the form of feed entries based on the Atom Syndication Format. These messages correspond to API-M method calls, indicating the name of the method, its parameters, return value, and other information about the method. Each message will be similar to the following example:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<?xml version="1.0" encoding="UTF-8"?> <entry xmlns="http://www.w3.org/2005/Atom" xmlns:fedora-types="http://www.fedora.info/definitions/1/0/types/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <id>urn:uuid:3773e144-1b63-4dde-8786-464243af9186</id> <updated>2008-04-14T22:35:13.953Z</updated> <author> <name>fedoraAdmin</name> <uri>http://localhost:8080/fedora</uri> </author> <title type="text">purgeObject</title> <category term="demo:5" scheme="fedora-types:pid" label="xsd:string"></category> <category term="purge message" scheme="fedora-types:logMessage" label="xsd:string"></category> <category term="false" scheme="fedora-types:force" label="xsd:boolean"></category> <summary type="text">demo:5</summary> <content type="text">2008-04-14T22:35:13.953Z</content> </entry> |
...
- You will need to make a few jars available to Fedora so it can load and process the ActiveMQ configuration file. Drop the jars
xbean-spring-3.4.3.jar
andspring-context-2.5.6.jar
files into the Fedora webapp WEB-INF/lib directory. Most spring applications provide these jars; if you are using Apache Servicemix as the container for your remote broker, they can be found in theSERVICEMIX_HOME/lib
directory. - Create the Fedora
activemq.xml
file, put it inFEDORA_HOME/server/config
.
This piece of the config file creates the bridge to the remote broker, configured in failover mode:Code Block xml xml borderStyle solidxml <beans xmlns:amq="http://activemq.apache.org/schema/core"> <!-- ActiveMQ JMS Broker configuration --> <amq:broker id="broker" useShutdownHook="false"> <amq:managementContext> <amq:managementContext connectorPort="1093" createConnector="false"/> </amq:managementContext> <!-- Your remote broker, configured with failover --> <amq:networkConnectors> <amq:networkConnector uri="static:(failover:(tcp://0.0.0.0:61617))"/> </amq:networkConnectors> <!-- The directory where Fedora will store the ActiveMQ data --> <amq:persistenceAdapter> <amq:amqPersistenceAdapter directory="file:./data/amq"/> </amq:persistenceAdapter> </amq:broker> <!-- Set this to prevent objects from being serialized when passed along to your embedded broker; saves some overhead processing --> <bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="objectMessageSerializationDefered" value="false"/> </bean> </beans>
Code Block xml xml borderStyle solidxml <amq:networkConnectors> <amq:networkConnector uri="static:(failover:(tcp://0.0.0.0:61617))"/> </amq:networkConnectors>
- Configure Fedora to read the
activemq.xml
file upon startup, create the embedded broker.
Make sure messaging is enabled infedora.fcfg
:
Also change theCode Block xml xml borderStyle solidxml <module role="org.fcrepo.server.messaging.Messaging" class="org.fcrepo.server.messaging.MessagingModule"> <comment>Fedora's Java Messaging Service (JMS) Module</comment> <param name="enabled" value="true"/> [...]
java.naming.provider.url
parameter:Code Block xml xml borderStyle solidxml <param name="java.naming.provider.url" value="vm://localhost?brokerConfig=xbean:file:/path/to/fedora_home/server/config/activemq.xml"/>
- Restart Fedora, and start up your remote broker (or the other way around: it no longer matters).
The ActiveMQ failover transport documentation contains a list of useful parameters that you can use to configure connection management to the remote broker (max reconnect attempts, period between reconnects, etc.)
...