Created
August 2, 2013 20:11
-
-
Save christian-posta/6143043 to your computer and use it in GitHub Desktop.
Review of DLQ test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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.broker.BrokerFactory; | |
import org.apache.activemq.broker.BrokerService; | |
import org.apache.activemq.broker.region.policy.IndividualDeadLetterStrategy; | |
import org.apache.activemq.broker.region.policy.PolicyEntry; | |
import org.apache.activemq.broker.region.policy.PolicyMap; | |
import org.apache.activemq.command.ActiveMQQueue; | |
import org.apache.activemq.command.ActiveMQTopic; | |
import org.junit.After; | |
import org.junit.Before; | |
import org.junit.Test; | |
import javax.jms.*; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
/** | |
* @author <a href="http://www.christianposta.com/blog">Christian Posta</a> | |
*/ | |
public class RobertsTestFromIrc { | |
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost"); | |
Connection connection; | |
Session session; | |
BrokerService brokerService; | |
MessageProducer producer; | |
String text; | |
@Before | |
public void setUp() throws Exception { | |
createBroker(); | |
try { | |
connection = connectionFactory.createConnection(); | |
connection.start(); | |
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); | |
Destination destination = session.createQueue("FOO.BAR"); | |
producer = session.createProducer(destination); | |
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); | |
} catch (JMSException ex) { | |
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
private void createBroker() throws Exception { | |
brokerService = BrokerFactory.createBroker("broker:(vm://localhost)/localhost?persistent=true"); | |
brokerService.setDeleteAllMessagesOnStartup(true); | |
brokerService.getSystemUsage().getMemoryUsage().setLimit(8 * 1024 * 1024); | |
configureBroker(brokerService); | |
brokerService.start(); | |
} | |
private void configureBroker(BrokerService broker) { | |
PolicyMap map = new PolicyMap(); | |
map.put(new ActiveMQQueue(">"), queuePolicy(">")); | |
map.put(new ActiveMQTopic(">"), topicPolicy(">")); | |
PolicyEntry foobarEntry = new PolicyEntry(); | |
foobarEntry.setQueue("FOO.BAR"); | |
IndividualDeadLetterStrategy strategy = new IndividualDeadLetterStrategy(); | |
strategy.setQueuePrefix("DLQ."); | |
strategy.setUseQueueForQueueMessages(true); | |
strategy.setProcessExpired(true); | |
strategy.setProcessNonPersistent(true); | |
foobarEntry.setDeadLetterStrategy(strategy); | |
map.put(new ActiveMQQueue("FOO.BAR"), foobarEntry); | |
broker.setDestinationPolicy(map); | |
} | |
private PolicyEntry topicPolicy(String name) { | |
PolicyEntry rc = new PolicyEntry(); | |
rc.setTopic(name); | |
rc.setProducerFlowControl(false); | |
return rc; | |
} | |
private PolicyEntry queuePolicy(String name) { | |
PolicyEntry rc = new PolicyEntry(); | |
rc.setQueue(name); | |
rc.setProducerFlowControl(false); | |
return rc; | |
} | |
@Test(timeout = 100000) | |
public void test() throws Exception { | |
int i; | |
for (i = 0; i < 10000000; i++) { | |
producer.send(session.createTextMessage(generateText(128 * 1024)), producer.getDeliveryMode(), Message.DEFAULT_PRIORITY, 1000); | |
if (i % 10000 == 0) { | |
System.out.println(i); | |
} | |
} | |
System.out.println(i); | |
} | |
private String generateText(int i) { | |
if (text == null) { | |
StringBuilder builder = new StringBuilder(i); | |
for (int j = 0; j < i; j++) { | |
builder.append("X"); | |
} | |
text = builder.toString(); | |
} | |
return text; | |
} | |
@After | |
public void tearDown() { | |
try { | |
session.close(); | |
connection.close(); | |
} catch (JMSException ex) { | |
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment