Skip to main content

RabbitMQ - Web Application Edition

Demonstrates using a rabbitmq client in a web application. For more usage, refer to dawdler-client-plug-rabbitmq.

1. Download and Run​

Download the sample dawdler-tutorial.zip.

After extracting it, go into the rabbitmq-single-web directory.

Start rabbitmq and configure rabbitmq.properties.

Run the main method of UserWebApplication.java.

@DawdlerBootApplication
public class UserWebApplication {
public static void main(String[] args) throws Throwable {
DawdlerWebApplication.run(UserWebApplication.class, args);
}
}

1.1 Configuration Files​

The rabbitmq connection configuration in rabbitmq.properties can also be stored in the unified configuration center.

For related configuration, refer to dawdler-rabbitmq-core.

1.2 Running as a Jar​

Run

mvn clean package

to get rabbitmq-single-web-0.0.1-jdk25-SNAPSHOT-all.jar

Then run

java -jar rabbitmq-single-web-0.0.1-jdk25-SNAPSHOT-all.jar

1.3 Verification​

Visit http://localhost:8085/user/sendMessage?msg=helloWorld with postman or a browser.

After a successful visit, UserWebListener.java

@RabbitListener(fileName="rabbitmq", prefetchCount=1, queueName="demo.queue", autoAck=false,concurrentConsumers = 2)
public void consume(Message message) throws Exception {
System.out.println("receive message: " + new String(message.getBody()));
}

will print receive message: helloWorld to the console.