import org.apache.activemq.broker.region.policy.PolicyEntry; //導入方法依賴的package包/類
@Override
public void before() throws Throwable {
log.info("+++ BEFORE on JUnit Rule '" + id(Rule_Mats.class) + "', JMS and MATS:");
String sysprop_matsTestActiveMq = System.getProperty(SYSPROP_MATS_TEST_ACTIVEMQ);
// ::: Server (BrokerService)
// ====================================
// :? Do we have specific brokerUrl to connect to?
if (sysprop_matsTestActiveMq == null) {
// -> No - the system property was not set, hence start the in-vm broker.
log.info("Setting up in-vm ActiveMQ BrokerService '" + BROKER_NAME + "' (i.e. the MQ server).");
_amqServer = new BrokerService();
_amqServer.setBrokerName(BROKER_NAME);
_amqServer.setUseJmx(false); // No need for JMX registry.
_amqServer.setPersistent(false); // No need for persistence (prevents KahaDB dirs from being created).
_amqServer.setAdvisorySupport(false); // No need Advisory Messages.
_amqServer.setUseShutdownHook(false);
// :: Set Individual DLQ
// Hear, hear: http://activemq.2283324.n4.nabble.com/PolicyMap-api-is-really-bad-td4284307.html
PolicyMap destinationPolicy = new PolicyMap();
_amqServer.setDestinationPolicy(destinationPolicy);
PolicyEntry policyEntry = new PolicyEntry();
policyEntry.setQueue(">");
destinationPolicy.put(policyEntry.getDestination(), policyEntry);
IndividualDeadLetterStrategy individualDeadLetterStrategy = new IndividualDeadLetterStrategy();
individualDeadLetterStrategy.setQueuePrefix("DLQ.");
policyEntry.setDeadLetterStrategy(individualDeadLetterStrategy);
_amqServer.start();
}
else {
// -> Yes, there is specified a brokerUrl to connect to, so we
log.info("SKIPPING setup of in-vm ActiveMQ BrokerService (MQ server), since System Property '"
+ SYSPROP_MATS_TEST_ACTIVEMQ + "' was set (to [" + sysprop_matsTestActiveMq + "]).");
}
// ::: Client (ConnectionFactory)
// ====================================
// :: Find which broker URL to use
String brokerUrl;
if (sysprop_matsTestActiveMq == null) {
brokerUrl = "vm://" + BROKER_NAME + "?create=false";
}
else if (SYSPROP_VALUE_LOCALHOST.equals(sysprop_matsTestActiveMq)) {
brokerUrl = "tcp://localhost:61616";
}
else {
brokerUrl = sysprop_matsTestActiveMq;
}
// :: Connect to the broker
log.info("Setting up ActiveMQ ConnectionFactory (MQ client), brokerUrl: [" + brokerUrl + "].");
_amqClient = new ActiveMQConnectionFactory(brokerUrl);
RedeliveryPolicy redeliveryPolicy = _amqClient.getRedeliveryPolicy();
// :: Only try redelivery once, since the unit tests does not need any more to prove that they work.
redeliveryPolicy.setInitialRedeliveryDelay(500);
redeliveryPolicy.setUseExponentialBackOff(false);
redeliveryPolicy.setMaximumRedeliveries(1);
// ::: MatsFactory
// ====================================
log.info("Setting up JmsMatsFactory.");
_matsStringSerializer = new MatsDefaultJsonSerializer();
// Allow for override in specialization classes, in particular the one with DB.
_matsFactory = createMatsFactory(_matsStringSerializer, _amqClient);
// For all test scenarios, it makes no sense to have a concurrency more than 1, unless explicitly testing that.
_matsFactory.getFactoryConfig().setConcurrency(1);
log.info("--- BEFORE done! JUnit Rule '" + id(Rule_Mats.class) + "', JMS and MATS.");
}
2384

被折叠的 条评论
为什么被折叠?



