use of org.apache.activemq.ActiveMQConnectionFactory in project activemq-artemis by apache.
the class MessageListenerExample method main.
public static void main(final String[] args) throws Exception {
Connection connection = null;
try {
ConnectionFactory cf = new ActiveMQConnectionFactory();
connection = cf.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue("exampleQueue");
int nMessages = 1000;
CountDownLatch latch = new CountDownLatch(nMessages);
MessageConsumer messageConsumer = session.createConsumer(queue);
messageConsumer.setMessageListener(new LocalListener(latch));
connection.start();
MessageProducer producer = session.createProducer(queue);
for (int i = 0; i < 1000; i++) {
TextMessage message = session.createTextMessage("This is a text message " + i);
System.out.println("Sent message: " + message.getText());
producer.send(message);
}
if (!latch.await(5, TimeUnit.SECONDS)) {
throw new RuntimeException("listener didn't receive all the messages");
}
System.out.println("Finished ok!");
} finally {
if (connection != null) {
connection.close();
}
}
}
use of org.apache.activemq.ActiveMQConnectionFactory in project activemq-artemis by apache.
the class NetworkRestartTest method doSetUp.
protected void doSetUp() throws Exception {
remoteBroker = createRemoteBroker();
remoteBroker.start();
remoteBroker.waitUntilStarted();
localBroker = createLocalBroker();
localBroker.start();
localBroker.waitUntilStarted();
String localURI = "tcp://localhost:61616";
String remoteURI = "tcp://localhost:61617";
ActiveMQConnectionFactory fac = new ActiveMQConnectionFactory(localURI);
localConnection = fac.createConnection();
localConnection.setClientID("local");
localConnection.start();
fac = new ActiveMQConnectionFactory(remoteURI);
fac.setWatchTopicAdvisories(false);
remoteConnection = fac.createConnection();
remoteConnection.setClientID("remote");
remoteConnection.start();
localSession = localConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
remoteSession = remoteConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
}
use of org.apache.activemq.ActiveMQConnectionFactory in project activemq-artemis by apache.
the class FailoverStaticNetworkTest method createConnectionFactory.
protected ConnectionFactory createConnectionFactory(final BrokerService broker) throws Exception {
String url = broker.getTransportConnectors().get(0).getServer().getConnectURI().toString();
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
connectionFactory.setOptimizedMessageDispatch(true);
connectionFactory.setDispatchAsync(false);
connectionFactory.setUseAsyncSend(false);
connectionFactory.setOptimizeAcknowledge(false);
connectionFactory.setAlwaysSyncSend(true);
return connectionFactory;
}
use of org.apache.activemq.ActiveMQConnectionFactory in project activemq-artemis by apache.
the class MessageCompressionTest method testStreamMessageCompression.
public void testStreamMessageCompression() throws Exception {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(connectionUri);
factory.setUseCompression(true);
sendTestStreamMessage(factory, TEXT);
ActiveMQStreamMessage streamMessage = receiveTestStreamMessage(factory);
int compressedSize = streamMessage.getContent().getLength();
assertTrue(streamMessage.isCompressed());
boolean booleanVal = streamMessage.readBoolean();
assertTrue(booleanVal);
byte byteVal = streamMessage.readByte();
assertEquals((byte) 10, byteVal);
byte[] originVal = TEXT.getBytes();
byte[] bytesVal = new byte[originVal.length];
streamMessage.readBytes(bytesVal);
for (int i = 0; i < bytesVal.length; i++) {
assertTrue(bytesVal[i] == originVal[i]);
}
char charVal = streamMessage.readChar();
assertEquals('A', charVal);
double doubleVal = streamMessage.readDouble();
assertEquals(55.3D, doubleVal, 0.1D);
float floatVal = streamMessage.readFloat();
assertEquals(79.1F, floatVal, 0.1F);
int intVal = streamMessage.readInt();
assertEquals(37, intVal);
long longVal = streamMessage.readLong();
assertEquals(56652L, longVal);
Object objectVal = streamMessage.readObject();
Object origVal = new String("VVVV");
assertTrue(objectVal.equals(origVal));
short shortVal = streamMessage.readShort();
assertEquals((short) 333, shortVal);
String strVal = streamMessage.readString();
assertEquals(TEXT, strVal);
factory = new ActiveMQConnectionFactory(connectionUri);
factory.setUseCompression(false);
sendTestStreamMessage(factory, TEXT);
streamMessage = receiveTestStreamMessage(factory);
int unCompressedSize = streamMessage.getContent().getLength();
System.out.println("compressedSize: " + compressedSize + " un: " + unCompressedSize);
assertTrue("expected: compressed Size '" + compressedSize + "' < unCompressedSize '" + unCompressedSize + "'", compressedSize < unCompressedSize);
}
use of org.apache.activemq.ActiveMQConnectionFactory in project activemq-artemis by apache.
the class MessageCompressionTest method testTextMessageCompression.
public void testTextMessageCompression() throws Exception {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(connectionUri);
factory.setUseCompression(true);
sendTestMessage(factory, TEXT);
ActiveMQTextMessage message = receiveTestMessage(factory);
int compressedSize = message.getContent().getLength();
factory = new ActiveMQConnectionFactory(connectionUri);
factory.setUseCompression(false);
sendTestMessage(factory, TEXT);
message = receiveTestMessage(factory);
int unCompressedSize = message.getContent().getLength();
assertEquals(TEXT, message.getText());
assertTrue("expected: compressed Size '" + compressedSize + "' < unCompressedSize '" + unCompressedSize + "'", compressedSize < unCompressedSize);
}
Aggregations