篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RabbitMQ Explain in Detail相关的知识,希望对你有一定的参考价值。
“Hello World”(using the Java Client)
RabbitMQ is a message broker: it accepts and forwards messages.
A queue. Although messages flow through RabbitMQ and your applications, they can only be stored inside a queue. A queue is only bound by the host’s memory & disk limits, it’s essentially a large message buffer.
Note that the producer, consumer, and broker do not have to reside on the same host; indeed in most applications they don’t. An application can be both a producer and consumer, too.
Java Client Library
Download the client library and its dependencies (SLF4J API and SLF4J Simple). Copy those files in your working directory, along the tutorials Java files.
Send
We’ll call our message publisher (sender) Send and our message consumer (receiver) Recv. The publisher will connect to RabbitMQ, send a single message, then exit.
The connection abstracts the socket connection, and takes care of protocol version negotiation and authentication and so on for us. Here we connect to a RabbitMQ node on the local machine - hence the localhost. If we wanted to connect to a node on a different machine we’d simply specify its hostname or IP address here.
Next we create a channel, which is where most of the API for getting things done resides. Note we can use a try-with-resources statement because both Connection and Channel implement java.lang.AutoCloseable. This way we don’t need to close them explicitly in our code.
To send, we must declare a queue for us to send to; then we can publish a message to the queue, all of this in the try-with-resources statement:
Declaring a queue is idempotent - it will only be created if it doesn’t exist already. The message content is a byte array, so you can encode whatever you like there.
You may get this error when you try to compile the program: “javac’ is not recognized as an internal or external command, operable program or batch file“. This error occurs when the java path is not set in your system
If you get this error then you first need to set the path before compilation.
After compilation the .java file gets translated into the .class file(byte code). Now we can run the program.
java FirstJavaProgram
Note that you should not append the .java extension
importjava.applet.Applet;importjava.awt.Graphics;importjava.awt.Color;publicclassSimpleAppletextendsAppletString text ="I'm a simple applet";publicvoidinit()//....
packages
The applet code also has three import statements at the top. Applications of any size and all applets use import statements to access ready-made Java API classes in packages.
-This is true whether the Java API classes come in the Java platform download, from a third-party, or are classes you write yourself and store in a directory separate from the program. At compile time, a program uses import statements to locate and reference compiled Java API classes stored in packages elsewhere on the local or networked system. A compiled class in one package can have the same name as a compiled class in another package. The package name differentiates the two classes.
An application deployed in a JAR archive uses a manifest to describe the contents of the archive. For more information, refer to the Packaging Programs in JAR Files lesson.
Checking the CLASSPATH variable
(All platforms)
The CLASSPATH variable is one way to tell applications, including the JDK tools, where to look for user classes. (Classes that are part of the JRE, JDK platform, and extensions should be defined through other means, such as the bootstrap class path or the extensions directory.)
The preferred way to specify the class path is by using the -cp command line switch. This allows the CLASSPATH to be set individually for each application without affecting other applications. Setting the CLASSPATH can be tricky and should be performed with care.
指定类路径的首选方法是使用-cp命令行开关。
The default value of the class path is “.”, meaning that only the current directory is searched. Specifying either the CLASSPATH variable or the -cp command line switch overrides this value.
To check whether CLASSPATH is set on Microsoft Windows NT/2000/XP, execute the following:
C:> echo %CLASSPATH%
On Solaris or Linux, execute the following:
% echo $CLASSPATH
If CLASSPATH is not set you will get a CLASSPATH: Undefined variable error (Solaris or Linux) or simply %CLASSPATH% (Microsoft Windows NT/2000/XP).
To modify the CLASSPATH, use the same procedure you used for the PATH variable.
Class path wildcards allow you to include an entire directory of .jar files in the class path without explicitly naming them individually. For more information, including an explanation of class path wildcards, and a detailed description on how to clean up the CLASSPATH environment variable, see the Setting the Class Path technical note.
类路径通配符允许你在类路径中包括整个目录的.jar文件,而不需要明确地单独命名它们。
Answers to Questions: The Platform Environment
Questions
**Question 1.**A programmer installs a new library contained in a .jar file. In order to access the library from his code, he sets the CLASSPATH environment variable to point to the new .jar file. Now he finds that he gets an error message when he tries to launch simple applications:
java Hello
Exception in thread "main" java.lang.NoClassDefFoundError: Hello
In this case, the Hello class is compiled into a .class file in the current directory — yet the java command can’t seem to find it. What’s going wrong?
Answer 1. A class is only found if it appears in the class path. By default, the class path consists of the current directory. If the CLASSPATH environment variable is set, and doesn’t include the current directory, the launcher can no longer find classes in the current directory. The solution is to change the CLASSPATH variable to include the current directory. For example, if the CLASSPATH value is c:\\java\\newLibrary.jar (Windows) or /home/me/newLibrary.jar (UNIX or Linux) it needs to be changed to .;c:\\java\\newLibrary.jar or .:/home/me/newLibrary.jar.
Putting it all together
You can compile both of these with just the RabbitMQ java client on the classpath:
On Windows, use a semicolon instead of a colon to separate items in the classpath.
The consumer will keep running, waiting for messages (Use Ctrl-C to stop it), so try running the publisher from another terminal.
x
Messaging with RabbitMQ - SpringBoot
This guide walks you through the process of setting up a RabbitMQ AMQP server that publishes and subscribes to messages and creating a Spring Boot application to interact with that RabbitMQ server.