java 动态队列_java - 在Java中动态创建异步消息队列 - 堆栈内存溢出

这篇博客展示了如何在Java中使用并发和队列数据结构动态创建异步消息队列,特别是针对发送邮件的场景。通过ExecutorService和不同的队列类型(如LinkedBlockingDeque和ArrayDeque),实现了将邮件分配到正确的邮件服务器进行发送的功能。测试用例确保了邮件由正确的服务器发送。
摘要由CSDN通过智能技术生成

我同意Adam,用例听起来像JMS是开销。 Java内置功能足够:

package de.mhaller;

import java.util.ArrayDeque;

import java.util.ArrayList;

import java.util.Deque;

import java.util.HashMap;

import java.util.Map;

import java.util.Queue;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.LinkedBlockingDeque;

import org.junit.Assert;

import org.junit.Test;

public class Mailer {

@Test

public void testMailer() throws Exception {

ExecutorService executor = Executors.newCachedThreadPool();

ArrayList log = new ArrayList();

LinkedBlockingDeque incoming = new LinkedBlockingDeque();

// TODO: Put mails to be sent into the incoming queue

incoming.offer(new Mail("foo1@localhost", "localhost"));

incoming.offer(new Mail("foo2@otherhost", "otherhost"));

incoming.offer(new Mail("foo3@otherhost", "otherhost"));

incoming.offer(new Mail("foo4@localhost", "localhost"));

Map> queues = new HashMap>();

while (!incoming.isEmpty()) {

Mail mail = incoming.pollFirst();

Mailserver mailserver = findMailserver(mail);

if (!queues.containsKey(mailserver)) {

ArrayDeque serverQueue = new ArrayDeque();

queues.put(mailserver, serverQueue);

executor.execute(new SendMail(mailserver, serverQueue));

}

Queue slot = queues.get(mailserver);

slot.offer(mail);

}

assertMailSentWithCorrectServer(log);

}

private void assertMailSentWithCorrectServer(ArrayList log) {

for (Mail mail : log) {

if (!mail.server.equals(mail.sentBy.mailserver)) {

Assert.fail("Mail sent by wrong server: " + mail);

}

}

}

private Mailserver findMailserver(Mail mail) {

// TODO: Your lookup logic which server to use

return new Mailserver(mail.server);

}

private static class Mail {

String recipient;

String server;

SendMail sentBy;

public Mail(String recipient, String server) {

this.recipient = recipient;

this.server = server;

}

@Override

public String toString() {

return "mail for " + recipient;

}

}

public static class SendMail implements Runnable {

private final Deque queue;

private final Mailserver mailserver;

public SendMail(Mailserver mailserver, Deque queue) {

this.mailserver = mailserver;

this.queue = queue;

}

@Override

public void run() {

while (!queue.isEmpty()) {

Mail mail = queue.pollFirst();

// TODO: Use SMTP to send the mail via mailserver

System.out.println(this + " sent " + mail + " via " + mailserver);

mail.sentBy = this;

}

}

}

public static class Mailserver {

String hostname;

public Mailserver(String hostname) {

this.hostname = hostname;

}

@Override

public String toString() {

return hostname;

}

@Override

public int hashCode() {

return hostname.hashCode();

}

@Override

public boolean equals(Object obj) {

return hostname.equals(((Mailserver) obj).hostname);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值