Define these in hornetq-jms.xml:
<queue name="A">
<entry name="A" />
</queue>
<queue name="B">
<entry name="B" />
</queue>
Execute this code:
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
props.put("java.naming.provider.url", "127.0.0.1:1099");
props.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
InitialContext initialcontext = new InitialContext(props);
QueueConnectionFactory jbosscf = (QueueConnectionFactory) initialcontext.lookup("ConnectionFactory");
Queue a = (Queue) initialcontext.lookup("A");
Queue b = (Queue) initialcontext.lookup("B");
QueueConnection connection = jbosscf.createQueueConnection();
connection.start();
QueueSession session;
// Send the same message to both "A" and "B"
session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
String text = "coucou at " + new Date();
TextMessage message = session.createTextMessage(text);
message.setStringProperty("_HQ_DUPL_ID", text);
MessageProducer producer;
producer = session.createProducer(a);
producer.send(message);
producer.close();
producer = session.createProducer(b);
producer.send(message);
producer.close();
QueueReceiver receiver;
receiver = session.createReceiver(a);
message = (TextMessage) receiver.receive(1000);
// Assert.assertEquals(text, message.getText());
receiver.close();
session.close();
connection.close();
String moveMode = "JMS";
// String moveMode = "MBEAN";
if (moveMode.equals("JMS"))
{
connection = jbosscf.createQueueConnection();
connection.start();
session = connection.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(a);
receiver = session.createReceiver(b);
message = (TextMessage) receiver.receive(1000);
// Assert.assertEquals(text, message.getText());
producer.send(message);
session.commit();
session.close();
connection.close();
}
else if (moveMode.equals("MBEAN")) {
JMXServiceURL jmxURL = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:5004/jmxrmi");
Hashtable<String, Object> h = new Hashtable<String, Object>();
String[] creds = new String[]
{ "admin", "admin" }
;
h.put(JMXConnector.CREDENTIALS, creds);
JMXConnector connector = JMXConnectorFactory.connect(jmxURL, h);
MBeanServerConnection serverConnection = connector.getMBeanServerConnection();
ObjectName on = new ObjectName("org.hornetq:module=JMS,name=\"B\",type=Queue");
Object[] myargs = new Object[]
{ "", "A" }
;
String[] sig = new String[]
{ "java.lang.String", "java.lang.String" }
;
serverConnection.invoke(on, "moveMessages", myargs, sig);
}
// log.info("done");
}