Skip to content

Commit ed96db0

Browse files
tabish121gemmellr
authored andcommitted
ARTEMIS-5489 Add a basic AMQP bridge example for broker connections
Adds an example that shows bridging from and to on a single broker connection.
1 parent 7f3d900 commit ed96db0

File tree

7 files changed

+476
-0
lines changed

7 files changed

+476
-0
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
<?xml version='1.0'?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one
4+
or more contributor license agreements. See the NOTICE file
5+
distributed with this work for additional information
6+
regarding copyright ownership. The ASF licenses this file
7+
to you under the Apache License, Version 2.0 (the
8+
"License"); you may not use this file except in compliance
9+
with the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing,
14+
software distributed under the License is distributed on an
15+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
KIND, either express or implied. See the License for the
17+
specific language governing permissions and limitations
18+
under the License.
19+
-->
20+
21+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
22+
<modelVersion>4.0.0</modelVersion>
23+
24+
<parent>
25+
<groupId>org.apache.activemq.examples.broker-connection</groupId>
26+
<artifactId>broker-connections</artifactId>
27+
<version>2.42.0-SNAPSHOT</version>
28+
</parent>
29+
30+
<artifactId>amqp-bridge</artifactId>
31+
<packaging>jar</packaging>
32+
<name>amqp-bridge</name>
33+
34+
<properties>
35+
<activemq.basedir>${project.basedir}/../../../..</activemq.basedir>
36+
</properties>
37+
38+
<dependencies>
39+
<dependency>
40+
<groupId>org.apache.qpid</groupId>
41+
<artifactId>qpid-jms-client</artifactId>
42+
</dependency>
43+
</dependencies>
44+
45+
<build>
46+
<plugins>
47+
<plugin>
48+
<groupId>org.apache.activemq</groupId>
49+
<artifactId>artemis-maven-plugin</artifactId>
50+
<executions>
51+
<execution>
52+
<id>create0</id>
53+
<goals>
54+
<goal>create</goal>
55+
</goals>
56+
<configuration>
57+
<ignore>${noServer}</ignore>
58+
<instance>${basedir}/target/server0</instance>
59+
<allowAnonymous>true</allowAnonymous>
60+
<configuration>${basedir}/target/classes/activemq/server0</configuration>
61+
<!-- this makes it easier in certain envs -->
62+
<javaOptions>-Djava.net.preferIPv4Stack=true</javaOptions>
63+
</configuration>
64+
</execution>
65+
<execution>
66+
<id>create1</id>
67+
<goals>
68+
<goal>create</goal>
69+
</goals>
70+
<configuration>
71+
<ignore>${noServer}</ignore>
72+
<instance>${basedir}/target/server1</instance>
73+
<allowAnonymous>true</allowAnonymous>
74+
<configuration>${basedir}/target/classes/activemq/server1</configuration>
75+
<!-- this makes it easier in certain envs -->
76+
<javaOptions>-Djava.net.preferIPv4Stack=true</javaOptions>
77+
</configuration>
78+
</execution>
79+
<!-- we first start broker 1, to avoid reconnecting statements -->
80+
<execution>
81+
<id>start1</id>
82+
<goals>
83+
<goal>cli</goal>
84+
</goals>
85+
<configuration>
86+
<ignore>${noServer}</ignore>
87+
<spawn>true</spawn>
88+
<location>${basedir}/target/server1</location>
89+
<testURI>tcp://localhost:5771</testURI>
90+
<args>
91+
<param>run</param>
92+
</args>
93+
<name>server1</name>
94+
</configuration>
95+
</execution>
96+
<execution>
97+
<id>start0</id>
98+
<goals>
99+
<goal>cli</goal>
100+
</goals>
101+
<configuration>
102+
<spawn>true</spawn>
103+
<ignore>${noServer}</ignore>
104+
<location>${basedir}/target/server0</location>
105+
<testURI>tcp://localhost:5660</testURI>
106+
<args>
107+
<param>run</param>
108+
</args>
109+
<name>server0</name>
110+
</configuration>
111+
</execution>
112+
<execution>
113+
<id>runClient</id>
114+
<goals>
115+
<goal>runClient</goal>
116+
</goals>
117+
<configuration>
118+
<!-- you may have to set export MAVEN_OPTS="-Djava.net.preferIPv4Stack=true"
119+
if you are on MacOS for instance -->
120+
<clientClass>org.apache.activemq.artemis.jms.example.BrokerBridgeExample</clientClass>
121+
</configuration>
122+
</execution>
123+
<execution>
124+
<id>stop0</id>
125+
<goals>
126+
<goal>stop</goal>
127+
</goals>
128+
<configuration>
129+
<ignore>${noServer}</ignore>
130+
<location>${basedir}/target/server0</location>
131+
</configuration>
132+
</execution>
133+
<execution>
134+
<id>stop1</id>
135+
<goals>
136+
<goal>stop</goal>
137+
</goals>
138+
<configuration>
139+
<ignore>${noServer}</ignore>
140+
<location>${basedir}/target/server1</location>
141+
</configuration>
142+
</execution>
143+
</executions>
144+
<dependencies>
145+
<dependency>
146+
<groupId>org.apache.activemq.examples.broker-connection</groupId>
147+
<artifactId>amqp-bridge</artifactId>
148+
<version>${project.version}</version>
149+
</dependency>
150+
</dependencies>
151+
</plugin>
152+
<plugin>
153+
<groupId>org.apache.maven.plugins</groupId>
154+
<artifactId>maven-clean-plugin</artifactId>
155+
</plugin>
156+
</plugins>
157+
</build>
158+
</project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# AMQP Broker Connection with bridge from and bridge to configurations
2+
3+
If you have not already done so, [prepare the broker distribution](../../../../README.md#getting-started) before running the example.
4+
5+
To run the example, simply type **mvn verify** from this directory, or **mvn -PnoServer verify** if you want to create and start the broker manually.
6+
7+
This example demonstrates how you can bridge messages sent to an Address on a remote server back to the local server and also instruct the local server to bridge messages sent to a Queue on the local server to a queue on the remote broker over single AMQP connection.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.activemq.artemis.jms.example;
18+
19+
import javax.jms.Connection;
20+
import javax.jms.ConnectionFactory;
21+
import javax.jms.MessageConsumer;
22+
import javax.jms.MessageProducer;
23+
import javax.jms.Queue;
24+
import javax.jms.Session;
25+
import javax.jms.TextMessage;
26+
import javax.jms.Topic;
27+
28+
import org.apache.qpid.jms.JmsConnectionFactory;
29+
30+
/**
31+
* This example is demonstrating how messages are bridge between two brokers with the
32+
* bridge configuration located on only one broker (server0) and only a single outbound
33+
* connection is configured from server0 to server1
34+
*/
35+
public class BrokerBridgeExample {
36+
37+
public static void main(final String[] args) throws Exception {
38+
final ConnectionFactory connectionFactoryServer0 = new JmsConnectionFactory("amqp://localhost:5660");
39+
final ConnectionFactory connectionFactoryServer1 = new JmsConnectionFactory("amqp://localhost:5771");
40+
41+
final Connection connectionOnServer0 = connectionFactoryServer0.createConnection();
42+
final Connection connectionOnServer1 = connectionFactoryServer1.createConnection();
43+
44+
connectionOnServer0.start();
45+
connectionOnServer1.start();
46+
47+
final Session sessionOnServer0 = connectionOnServer0.createSession(Session.AUTO_ACKNOWLEDGE);
48+
final Session sessionOnServer1 = connectionOnServer1.createSession(Session.AUTO_ACKNOWLEDGE);
49+
50+
final Topic ordersTopic = sessionOnServer0.createTopic("orders");
51+
final Queue trackingQueue = sessionOnServer1.createQueue("tracking");
52+
53+
// Create consumers which generate demand on tracked resources and create bridge links
54+
final MessageConsumer ordersConsumerOn0 = sessionOnServer0.createConsumer(ordersTopic);
55+
final MessageConsumer trackingConsumerOn1 = sessionOnServer1.createConsumer(trackingQueue);
56+
57+
// Bridge from server0 to server1 on the tracking queue
58+
final MessageProducer trackingProducerOn0 = sessionOnServer0.createProducer(trackingQueue);
59+
60+
final TextMessage trackingMessageSent = sessionOnServer0.createTextMessage("new-tracking-data");
61+
62+
trackingProducerOn0.send(trackingMessageSent);
63+
64+
final TextMessage trackingMessageReceived = (TextMessage) trackingConsumerOn1.receive(5_000);
65+
66+
System.out.println("Consumer on server 1 received tracking data from producer on server 0 " + trackingMessageReceived.getText());
67+
68+
// Bridge from server1 back to server0 on the orders address
69+
final MessageProducer ordersProducerOn1 = sessionOnServer1.createProducer(ordersTopic);
70+
71+
final TextMessage orderMessageSent = sessionOnServer1.createTextMessage("new-order");
72+
73+
ordersProducerOn1.send(orderMessageSent);
74+
75+
final TextMessage orderMessageReceived = (TextMessage) ordersConsumerOn0.receive(5_000);
76+
77+
System.out.println("Consumer on server 0 received order message from producer on server 1 " + orderMessageReceived.getText());
78+
}
79+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?xml version='1.0'?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one
4+
or more contributor license agreements. See the NOTICE file
5+
distributed with this work for additional information
6+
regarding copyright ownership. The ASF licenses this file
7+
to you under the Apache License, Version 2.0 (the
8+
"License"); you may not use this file except in compliance
9+
with the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing,
14+
software distributed under the License is distributed on an
15+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
KIND, either express or implied. See the License for the
17+
specific language governing permissions and limitations
18+
under the License.
19+
-->
20+
21+
<configuration xmlns="urn:activemq"
22+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xmlns:xi="http://www.w3.org/2001/XInclude"
24+
xsi:schemaLocation="urn:activemq /schema/artemis-configuration.xsd">
25+
26+
<core xmlns="urn:activemq:core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
27+
xsi:schemaLocation="urn:activemq:core ">
28+
29+
<name>0.0.0.0</name>
30+
31+
<persistence-enabled>false</persistence-enabled>
32+
33+
<journal-type>NIO</journal-type>
34+
35+
<!-- should the broker detect dead locks and other issues -->
36+
<critical-analyzer>true</critical-analyzer>
37+
38+
<critical-analyzer-timeout>120000</critical-analyzer-timeout>
39+
40+
<critical-analyzer-check-period>60000</critical-analyzer-check-period>
41+
42+
<critical-analyzer-policy>HALT</critical-analyzer-policy>
43+
44+
<page-sync-timeout>44000</page-sync-timeout>
45+
46+
<acceptors>
47+
<!-- Acceptor for every supported protocol -->
48+
<acceptor name="artemis">tcp://0.0.0.0:5660?tcpSendBufferSize=1048576;tcpReceiveBufferSize=1048576;amqpMinLargeMessageSize=102400;protocols=CORE,AMQP,STOMP,HORNETQ,MQTT,OPENWIRE;useEpoll=true;amqpCredits=1000;amqpLowCredits=300;amqpDuplicateDetection=true</acceptor>
49+
</acceptors>
50+
51+
<broker-connections>
52+
<amqp-connection uri="tcp://localhost:5771" name="bridge-example" retry-interval="100">
53+
<!-- This will create a bridge connection between servers, the local
54+
server will bridge messages sent to the address 'orders' from the
55+
remote and the local will bridge messages sent to the tracking queue
56+
on the local broker to the matching queue on the remote. -->
57+
<bridge name = "example-bridge-configuration">
58+
<bridge-to-queue name="bridge-to-remote-queue">
59+
<include address-match="#" queue-match="tracking" />
60+
</bridge-to-queue>
61+
<bridge-from-address name="bridge-from-remote-address">
62+
<include address-match="orders" />
63+
</bridge-from-address>
64+
</bridge>
65+
</amqp-connection>
66+
</broker-connections>
67+
68+
<security-settings>
69+
<security-setting match="#">
70+
<permission type="createNonDurableQueue" roles="guest"/>
71+
<permission type="deleteNonDurableQueue" roles="guest"/>
72+
<permission type="createDurableQueue" roles="guest"/>
73+
<permission type="deleteDurableQueue" roles="guest"/>
74+
<permission type="createAddress" roles="guest"/>
75+
<permission type="deleteAddress" roles="guest"/>
76+
<permission type="consume" roles="guest"/>
77+
<permission type="browse" roles="guest"/>
78+
<permission type="send" roles="guest"/>
79+
<permission type="manage" roles="guest"/>
80+
</security-setting>
81+
</security-settings>
82+
83+
<address-settings>
84+
<!-- if you define auto-create on certain queues, management has to be auto-create -->
85+
<address-setting match="activemq.management#">
86+
<dead-letter-address>DLQ</dead-letter-address>
87+
<expiry-address>ExpiryQueue</expiry-address>
88+
<redelivery-delay>0</redelivery-delay>
89+
<!-- with -1 only the global-max-size is in use for limiting -->
90+
<max-size-bytes>-1</max-size-bytes>
91+
<message-counter-history-day-limit>10</message-counter-history-day-limit>
92+
<address-full-policy>PAGE</address-full-policy>
93+
<auto-create-queues>true</auto-create-queues>
94+
<auto-create-addresses>true</auto-create-addresses>
95+
</address-setting>
96+
<!--default for catch all-->
97+
<address-setting match="#">
98+
<dead-letter-address>DLQ</dead-letter-address>
99+
<expiry-address>ExpiryQueue</expiry-address>
100+
<redelivery-delay>0</redelivery-delay>
101+
<!-- with -1 only the global-max-size is in use for limiting -->
102+
<max-size-bytes>-1</max-size-bytes>
103+
<message-counter-history-day-limit>10</message-counter-history-day-limit>
104+
<address-full-policy>PAGE</address-full-policy>
105+
<auto-create-queues>true</auto-create-queues>
106+
<auto-create-addresses>true</auto-create-addresses>
107+
</address-setting>
108+
</address-settings>
109+
110+
<addresses>
111+
<address name="orders">
112+
<multicast>
113+
</multicast>
114+
</address>
115+
<address name="tracking">
116+
<anycast>
117+
<queue name="tracking" />
118+
</anycast>
119+
</address>
120+
</addresses>
121+
122+
</core>
123+
</configuration>

0 commit comments

Comments
 (0)