Skip to content

Commit

Permalink
Hacking stomp-jms example to solve "Peer disconnected"
Browse files Browse the repository at this point in the history
  • Loading branch information
themerius committed Oct 12, 2015
1 parent 1b49559 commit d1495b5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
1 change: 1 addition & 0 deletions examples/protocols/stomp/stomp-jms/readme.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ <h1>Stomp 1.2 Example</h1>

<pre>To run the example, simply type <b>mvn verify</b> from this directory, <br>or <b>mvn -PnoServer verify</b> if you want to start and create the server manually.</pre>

<pre>Start it with <em>mvn verify -Dtraffic=true<em> to send all second a message.</pre>

<p>This example shows you how to configure ActiveMQ Artemis to send and receive Stomp messages using Stomp 1.2 protocol.</p>
<p>The example will start a ActiveMQ Artemis server configured with Stomp and JMS.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
*/
package org.apache.activemq.artemis.jms.example;

import java.util.Timer;
import java.util.TimerTask;
import java.util.Date;

import javax.jms.Connection;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
Expand All @@ -37,29 +41,39 @@ public static void main(final String[] args) throws Exception {
factory.setTopicPrefix("jms.topic.");
factory.setBrokerURI("tcp://localhost:61616");
Connection connection = factory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
connection.start();

final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue("queue1");
MessageProducer producer = session.createProducer(queue);
final MessageProducer producer = session.createProducer(queue);
MessageConsumer consumer = session.createConsumer(queue);

producer.send(session.createTextMessage("Hello"));

connection.start();


System.out.println("Waiting 20 seconds");
System.out.println("Waiting 10 seconds");
Thread.sleep(10000); // increase this and it will fail
System.out.println("waited");

MessageConsumer consumer = session.createConsumer(queue);

TextMessage message = (TextMessage) consumer.receive(5000);

System.out.println("The content of the message is " + message.getText());

if (!message.getText().equals("Hello")) {
throw new IllegalStateException("the content of the message was different than expected!");
// send message all second.
if (System.getProperty("traffic") != null) {
Timer timer = new Timer();
timer.schedule( new TimerTask() {
public void run() {
try {
producer.send(session.createTextMessage("Hello from Timer @ " + new Date()));
} catch (Exception e) {
System.out.println("Oops");
}
}
}, 0, 1000);
}

connection.close();
// block until messages arrive.
// every time a message arrives, process it
while (true) {
TextMessage message = (TextMessage) consumer.receive();
System.out.println("The content of the message is " + message.getText());
}
}
}

0 comments on commit d1495b5

Please sign in to comment.