Skip to content

Instantly share code, notes, and snippets.

@christian-posta
Last active December 20, 2015 23:19
Show Gist options
  • Save christian-posta/6211175 to your computer and use it in GitHub Desktop.
Save christian-posta/6211175 to your computer and use it in GitHub Desktop.
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.usecases;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQSession;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.store.kahadb.KahaDBPersistenceAdapter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import javax.jms.*;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
/**
* @author <a href="http://www.christianposta.com/blog">Christian Posta</a>
*/
public class OrderingTest {
private static final String BROKER_URL = "failover:(tcp://127.0.0.1:61616)?initialReconnectDelay=" +
"100&useExponentialBackOff=false&maxReconnectDelay=10000&maxReconnectAttempts=1&timeout=10";
BrokerService broker;
@Before
public void setUp() throws Exception {
broker = new BrokerService();
configure(broker);
start(broker);
}
@After
public void tearDown() throws Exception {
stop(broker);
}
private void stop(BrokerService broker) throws Exception {
broker.stop();
broker.waitUntilStopped();
}
private void start(BrokerService broker) throws Exception {
broker.start();
broker.waitUntilStarted();
}
@Test
public void testOrdering() throws JMSException, InterruptedException {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(BROKER_URL);
factory.setAlwaysSessionAsync(true);
// set up producer
Connection producerConnection = factory.createConnection();
Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = producerSession.createQueue("DA_QUEUE");
MessageProducer producer = producerSession.createProducer(null);
producerConnection.start();
for (int i = 0; i < 30; i++) {
System.out.println("Send message: " + i);
producer.send(destination, producerSession.createTextMessage("Message # " + i));
}
producer.close();
producerSession.close();
producerConnection.close();
// let's consume...
Connection consumerConnection = factory.createConnection();
Session consumerSession = consumerConnection.createSession(false, ActiveMQSession.INDIVIDUAL_ACKNOWLEDGE);
ActiveMQQueue queue = new ActiveMQQueue("DA_QUEUE?consumer.prefetchSize=1");
MessageConsumer consumer = consumerSession.createConsumer(queue);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
if (message instanceof TextMessage) {
try {
int count = getCount((TextMessage)message);
System.out.println("Count: " + count);
if ((count % 3 == 0) && !message.getJMSRedelivered()) {
System.out.println("Will fail for: " + count);
throw new RuntimeException("Failed on purpose for: " + count);
}else {
message.acknowledge();
System.out.println("ACK'd message: " + count);
}
} catch (JMSException e) {
System.out.println("Error! ");
e.printStackTrace();
}
} else {
System.out.println("Got some weird kind of message: " + message.getClass());
}
}
private int getCount(TextMessage message) throws JMSException {
String[] array = ((TextMessage) message).getText().split(" ");
return Integer.parseInt(array[array.length - 1]);
}
});
consumerConnection.start();
TimeUnit.SECONDS.sleep(2);
}
private void configure(BrokerService broker) throws Exception {
configureStore(broker);
broker.setDeleteAllMessagesOnStartup(true);
broker.addConnector("tcp://127.0.0.1:61616");
}
private void configureStore(BrokerService broker) throws IOException {
KahaDBPersistenceAdapter store = new KahaDBPersistenceAdapter();
store.setJournalMaxFileLength(100 * 1024);
store.setIndexWriteBatchSize(100);
store.setEnableIndexWriteAsync(true);
broker.setPersistenceAdapter(store);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment