用java写一个队列发送消息
时间: 2023-04-06 10:00:44 浏览: 85
你好,我是C知道,关于您的问题,我可以回答。使用Java编写队列发送消息的代码可以使用Java Message Service (JMS) API。JMS是一种Java API,用于在应用程序之间发送消息。您可以使用ActiveMQ、RabbitMQ等消息队列中间件来实现JMS。具体实现方式可以参考相关文档和教程。希望能对您有所帮助。
相关问题
写一个java消息队列发送
消息的示例程序。
好的,以下是一个简单的 Java 消息队列发送消息的示例程序:
```
import javax.jms.*;
import org.apache.activemq.ActiveMQConnectionFactory;
public class MessageQueueSender {
private static final String BROKER_URL = "tcp://localhost:61616"; // ActiveMQ broker 地址
private static final String QUEUE_NAME = "test_queue"; // 消息队列名称
public static void main(String[] args) {
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(BROKER_URL);
try {
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(QUEUE_NAME);
MessageProducer producer = session.createProducer(queue);
// 创建一条文本消息
TextMessage message = session.createTextMessage("Hello, World!");
// 发送消息
producer.send(message);
System.out.println("Sent message: " + message.getText());
connection.close();
} catch (JMSException e) {
System.err.println("JMSException: " + e.getMessage());
}
}
}
```
这个程序使用了 ActiveMQ 作为消息队列中间件,可以将消息发送到指定的队列中。
首先,定义了 ActiveMQ broker 的地址 `BROKER_URL` 和消息队列名称 `QUEUE_NAME`。然后创建一个 `ActiveMQConnectionFactory` 来创建 `Connection`。
接下来,创建一个 `Session` 对象,并在创建 `Queue` 和 `MessageProducer` 对象。在这个例子中,创建了一条文本消息并用 `producer.send()` 方法发送出去。
最后,关闭连接。
希望这个示例程序对您有所帮助!
用java写一个消息队列
以下是一个简单的Java消息队列的实现:
```
import java.util.LinkedList;
public class MessageQueue {
private LinkedList<Object> queue;
public MessageQueue() {
queue = new LinkedList<>();
}
public synchronized void sendMessage(Object message) {
queue.addLast(message);
notify();
}
public synchronized Object receiveMessage() throws InterruptedException {
while (queue.isEmpty()) {
wait();
}
return queue.removeFirst();
}
}
```
此消息队列使用LinkedList作为底层实现。sendMessage方法向队列末尾添加消息,receiveMessage方法从队列头部删除消息并返回,如果队列为空则阻塞等待直到有消息可用。
在使用时,可以创建一个MessageQueue对象,并在多个线程中使用sendMessage和receiveMessage方法来发送和接收消息。例如:
```
public class Test {
public static void main(String[] args) {
MessageQueue queue = new MessageQueue();
Thread producer = new Thread(() -> {
for (int i = 0; i < 10; i++) {
queue.sendMessage("Message " + i);
}
});
Thread consumer = new Thread(() -> {
for (int i = 0; i < 10; i++) {
try {
Object message = queue.receiveMessage();
System.out.println("Received message: " + message);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
producer.start();
consumer.start();
}
}
```
此示例创建了一个生产者线程和一个消费者线程,生产者向消息队列中发送10个消息,消费者从消息队列中接收10个消息并输出。由于使用了同步机制,生产者和消费者线程可以安全地访问共享的消息队列。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)