Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reworked the StompClient example #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 38 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,67 +16,80 @@ compile group: 'org.fusesource.stompjms', name: 'stompjms-client', version: '1.1
``` java
package darwinStomp;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.Session;
import org.fusesource.stomp.jms.StompJmsConnectionFactory;
import javax.jms.*;

public class StompClient implements Runnable {

public static void main(String[] args) throws Exception {
private static final Logger LOG = Logger.getLogger(StompClient.class.getName());

private final String BROKER_URI = "tcp://datafeeds.nationalrail.co.uk:61613";
private final String QUEUE_NAME = "Your security key from My Feeds";

public static void main(String[] args) {
new StompClient().run();
}

@Override
public void run() {
String brokerUri = "tcp://datafeeds.nationalrail.co.uk:61613";
String QUEUE_NAME = "Your security key from My Feeds"

StompJmsConnectionFactory connectionFactory = new StompJmsConnectionFactory();
connectionFactory.setBrokerURI(brokerUri);

connectionFactory.setBrokerURI(this.BROKER_URI);

Connection connection = null;
Session session = null;
MessageConsumer consumer = null;

try {
connection = connectionFactory.createConnection("d3user", "d3password");
connection.start();

session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(QUEUE_NAME);
Queue queue = session.createQueue(this.QUEUE_NAME);
consumer = session.createConsumer(queue);

System.out.println("Connected to STOMP " + brokerUri);
System.out.println("Connected to STOMP " + this.BROKER_URI);

consumer.setMessageListener(new MessageHandler());

while (!Thread.interrupted()) {}
while (!Thread.interrupted()) {
}

try {
if (consumer != null) {
} catch (JMSException ex) {
Logger.getLogger(StompClient.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Thread interupted by exception or shutdown");
} finally {
if (consumer != null) {
try {
consumer.close();
} catch (JMSException ex) {
LOG.log(Level.SEVERE, null, ex);
}

if (session != null) {
}
if (session != null) {
try {
session.close();
} catch (JMSException ex) {
LOG.log(Level.SEVERE, null, ex);
}

if (connection != null) {
}
if (connection != null) {
try {
connection.close();
connection = null;
} catch (JMSException ex) {
LOG.log(Level.SEVERE, null, ex);
}
} catch (JMSException ex) {
System.out.println("Got exception on shutdown");
ex.printStackTrace();
}

System.out.println("Thread was interrupted!");


} catch (JMSException e) {
e.printStackTrace();
}
}
}

```

##### src/main/java/darwinStomp/MessageHandler
Expand Down