Skip to content

Instantly share code, notes, and snippets.

@clebertsuconic
Created February 21, 2020 23:35
Show Gist options
  • Save clebertsuconic/7d358d185e0fc46d02514d5a890a6f83 to your computer and use it in GitHub Desktop.
Save clebertsuconic/7d358d185e0fc46d02514d5a890a6f83 to your computer and use it in GitHub Desktop.
ublic class ClientSideLoadBalancingExample {
static int tcount = 0;
static class MyRunner extends Thread {
MyRunner() {
super("Thread " + (++tcount));
}
@Override
public void run() {
try {
for (int i = 0; i < 100; i++) {
InitialContext initialContext = new InitialContext();
Queue queue = (Queue) initialContext.lookup("queue/exampleQueue");
// Step 3. Look-up a JMS Connection Factory object from JNDI on server 0
ConnectionFactory connectionFactory = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
System.out.println(Thread.currentThread().getName() + " is running a connection at " + i);
Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
TextMessage messageA = session.createTextMessage("A:This is text message " + i);
MessageProducer producer = session.createProducer(queue);
producer.send(messageA);
connection.close();
Thread.sleep(1000);
}
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
}
public static void main(final String[] args) throws Exception {
for (int i = 0; i < 100; i++) {
Thread t = new MyRunner();
t.start();
}
while (true) {
Thread.sleep(10000);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment