I don't know what could be causing the problem but my code keeps failing when it get to this part
Connection connection = factory.createConnection(user, password);
This is the code I'm working on:
public static void main(String[] args) throws JMSException {
final String TOPIC_PREFIX = "topic://";
String user = env("ACTIVEMQ_USER", "admin");
String password = env("ACTIVEMQ_PASSWORD", "admin");
String host = env("ACTIVEMQ_HOST", "localhost");
int port = Integer.parseInt(env("ACTIVEMQ_PORT", "5672"));
String connectionURI = "amqp://" + host + ":" + port;
String destinationName = arg(args, 0, "stage");
JmsConnectionFactory factory = new JmsConnectionFactory(connectionURI);
Connection connection = factory.createConnection(user, password);
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = null;
if (destinationName.startsWith(TOPIC_PREFIX)) {
destination = session.createTopic(destinationName.substring(TOPIC_PREFIX.length()));
} else {
destination = session.createQueue(destinationName);
}
MessageConsumer consumer = session.createConsumer(destination);
long start = System.currentTimeMillis();
long count = 1;
System.out.println("Waiting for messages...");
while (true) {
Message msg = consumer.receive();
if (msg instanceof TextMessage) {
String body = ((TextMessage) msg).getText();
if ("SHUTDOWN".equals(body)) {
long diff = System.currentTimeMillis() - start;
System.out.println(String.format("Received %d in %.2f seconds", count, (1.0 * diff / 1000.0)));
connection.close();
try {
Thread.sleep(10);
} catch (Exception e) {}
System.exit(0);
} else {
System.out.println(String.format("Received %d messages.", msg.toString()));
}
} else {
System.out.println("Unexpected message type: " + msg.getClass());
}
}
}
This is the error message I keep getting:
/home/thabangligavha/.jdks/openjdk-18.0.2.1/bin/java -javaagent:/snap/intellij-idea-community/405/lib/idea_rt.jar=39811:/snap/intellij-idea-community/405/bin -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath /home/thabangligavha/apache-activemq-5.17.3/examples/amqp/java/target/classes:/home/thabangligavha/.m2/repository/org/apache/geronimo/specs/geronimo-jms_1.1_spec/1.1/geronimo-jms_1.1_spec-1.1.jar:/home/thabangligavha/.m2/repository/org/apache/qpid/qpid-jms-client/1.6.0/qpid-jms-client-1.6.0.jar:/home/thabangligavha/.m2/repository/org/slf4j/slf4j-api/1.7.36/slf4j-api-1.7.36.jar:/home/thabangligavha/.m2/repository/jakarta/jms/jakarta.jms-api/2.0.3/jakarta.jms-api-2.0.3.jar:/home/thabangligavha/.m2/repository/org/apache/qpid/proton-j/0.33.10/proton-j-0.33.10.jar:/home/thabangligavha/.m2/repository/io/netty/netty-buffer/4.1.75.Final/netty-buffer-4.1.75.Final.jar:/home/thabangligavha/.m2/repository/io/netty/netty-common/4.1.75.Final/netty-common-4.1.75.Final.jar:/home/thabangligavha/.m2/repository/io/netty/netty-handler/4.1.75.Final/netty-handler-4.1.75.Final.jar:/home/thabangligavha/.m2/repository/io/netty/netty-resolver/4.1.75.Final/netty-resolver-4.1.75.Final.jar:/home/thabangligavha/.m2/repository/io/netty/netty-codec/4.1.75.Final/netty-codec-4.1.75.Final.jar:/home/thabangligavha/.m2/repository/io/netty/netty-transport/4.1.75.Final/netty-transport-4.1.75.Final.jar:/home/thabangligavha/.m2/repository/io/netty/netty-transport-native-epoll/4.1.75.Final/netty-transport-native-epoll-4.1.75.Final-linux-x86_64.jar:/home/thabangligavha/.m2/repository/io/netty/netty-transport-native-unix-common/4.1.75.Final/netty-transport-native-unix-common-4.1.75.Final.jar:/home/thabangligavha/.m2/repository/io/netty/netty-transport-classes-epoll/4.1.75.Final/netty-transport-classes-epoll-4.1.75.Final.jar:/home/thabangligavha/.m2/repository/io/netty/netty-transport-native-kqueue/4.1.75.Final/netty-transport-native-kqueue-4.1.75.Final-osx-x86_64.jar:/home/thabangligavha/.m2/repository/io/netty/netty-transport-classes-kqueue/4.1.75.Final/netty-transport-classes-kqueue-4.1.75.Final.jar:/home/thabangligavha/.m2/repository/io/netty/netty-codec-http/4.1.75.Final/netty-codec-http-4.1.75.Final.jar example.Listener
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" javax.jms.JMSException: finishConnect(..) failed: Connection refused: localhost/127.0.0.1:5672
at org.apache.qpid.jms.provider.ProviderException.toJMSException(ProviderException.java:34)
at org.apache.qpid.jms.exceptions.JmsExceptionSupport.create(JmsExceptionSupport.java:80)
at org.apache.qpid.jms.exceptions.JmsExceptionSupport.create(JmsExceptionSupport.java:112)
at org.apache.qpid.jms.JmsConnection.connect(JmsConnection.java:176)
at org.apache.qpid.jms.JmsConnectionFactory.createConnection(JmsConnectionFactory.java:213)
at example.Listener.main(Listener.java:38)
Caused by: org.apache.qpid.jms.provider.exceptions.ProviderIOException: finishConnect(..) failed: Connection refused: localhost/127.0.0.1:5672
at org.apache.qpid.jms.provider.exceptions.ProviderExceptionSupport.createOrPassthroughFatal(ProviderExceptionSupport.java:46)
at org.apache.qpid.jms.provider.amqp.AmqpProvider.connect(AmqpProvider.java:309)
at org.apache.qpid.jms.JmsConnection.connect(JmsConnection.java:162)
... 2 more
The problem is this:
Connection refused: localhost/127.0.0.1:5672
This means that the Qpid JMS client implementation could not establish a connection to a JMS broker running on localhost port 5672
(ie the default port for AMQP). Please make sure you're running ActiveMQ on your localhost and it is listening for AMQP connections on port 5672
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.