}
}
**
direct模式
**
comsumer:消费者
package com.rosam.demo.rabbitmq.direct;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.ConsumerCancelledException;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.ShutdownSignalException;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rosam.demo.common.ExchangeType;
class Consumer {
private static final String QUEUE_NAME = “BOYS”;
private static final String QUEUE_NAME_2 = “GRILS”;
private static ConnectionFactory factory;
private static Connection connection;
private static Channel channel;
static{
try {
factory = new ConnectionFactory();
factory.setHost(“localhost”);
factory.setPort(5672);
factory.setVirtualHost(“/”);
factory.setUsername(“guest”);
factory.setPassword(“guest”);
connection = factory.newConnection();
channel = connection.createChannel();
channel.exchangeDeclare(ExchangeType.DIRECT.getName(), ExchangeType.DIRECT.getType());
/1.队列名称,2.是否可持久化,3.是否排他列队,4.是否自动删除(没有消费者订阅时)/
channel.queueDeclare(QUEUE_NAME, false, false, true, null);
channel.queueBind(QUEUE_NAME, ExchangeType.DIRECT.getName(), “boys”);
channel.queueDeclare(QUEUE_NAME_2, false, false, true, null);
channel.queueBind(QUEUE_NAME_2, ExchangeType.DIRECT.getName(), “girls”);
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
public static void receive() throws ShutdownSignalException, ConsumerCancelledException, InterruptedException, IOException{
com.rabbitmq.client.Consumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
throws IOException {
String msg = new String(body);
System.out.println(“cousumer received message:” + msg);
}
};
com.rabbitmq.client.Consumer consumer2 = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
throws IOException {
String msg = new String(body);
System.out.println(“cousumer2 received message:” + msg);
}
};
/1.队列名称;2.是否自动发送ack;3.消费者/
channel.basicConsume(QUEUE_NAME, true, consumer);
channel.basicConsume(QUEUE_NAME_2, true, consumer2);
}
public static void main(String[] args) throws IOException, TimeoutException {
try {
receive();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Producer:生产者
package com.rosam.demo.rabbitmq.direct;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;
import com.rosam.demo.common.ExchangeType;
public class Producer {
private static final String QUEUE_NAME = “BOYS”;
private static final String QUEUE_NAME_2 = “GRILS”;
private static ConnectionFactory factory;
private static Connection connection;
private static Channel channel;
static{
try {
factory = new ConnectionFactory();
factory.setHost(“localhost”);
factory.setPort(5672);
factory.setVirtualHost(“/”);
factory.setUsername(“guest”);
factory.setPassword(“guest”);
connection = factory.newConnection();
channel = connection.createChannel();
channel.exchangeDeclare(ExchangeType.DIRECT.getName(), ExchangeType.DIRECT.getType());
/1.队列名称,2.是否可持久化,3.是否排他列队,4.是否自动删除(空闲时)/
channel.queueDeclare(QUEUE_NAME, false, false, true, null);
channel.queueBind(QUEUE_NAME, ExchangeType.DIRECT.getName(), “boys”);
channel.queueDeclare(QUEUE_NAME_2, false, false, true, null);
channel.queueBind(QUEUE_NAME_2, ExchangeType.DIRECT.getName(), “girls”);
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
public static void send(String msg) throws IOException, TimeoutException{
try {
/1.交换机名称;2.路由关键字即routing key;3.配置信息,contentType、contentEncoding等;4.消息,字节数组格式/
channel.basicPublish(ExchangeType.DIRECT.getName(), “boys”, MessageProperties.MINIMAL_BASIC, msg.getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally{
channel.close();
connection.close();
}
}
public static void main(String[] args) {
try {
send(“hello rabbitmq”);
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
}
**
Topic模式
**
Producer:生产者
package com.rosam.demo.rabbitmq.topic;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rosam.demo.common.ExchangeType;
public class Producer {
private static final String QUEUE_NAME = “BOYS”;
private static final String QUEUE_NAME_2 = “GRILS”;
private static ConnectionFactory factory;
private static Connection connection;
private static Channel channel;
static{
try {
factory = new ConnectionFactory();
factory.setHost(“localhost”);
factory.setPort(5672);
factory.setVirtualHost(“/”);
factory.setUsername(“guest”);
factory.setPassword(“guest”);
connection = factory.newConnection();
channel = connection.createChannel();
channel.exchangeDeclare(ExchangeType.TOPIC.getName(), ExchangeType.TOPIC.getType());
/1.队列名称,2.是否可持久化,3.是否排他列队,4.是否自动删除(空闲时)/
channel.queueDeclare(QUEUE_NAME, false, false, true, null);
channel.queueBind(QUEUE_NAME, ExchangeType.TOPIC.getName(), “boys..”);
channel.queueDeclare(QUEUE_NAME_2, false, false, true, null);
channel.queueBind(QUEUE_NAME_2, ExchangeType.TOPIC.getName(), “..girls”);
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
public static void send(String msg) throws IOException, TimeoutException{
try {
/1.交换机名称;2.路由关键字即routing key;3;4.消息,字节数组格式/
channel.basicPublish(ExchangeType.TOPIC.getName(), “boys.like.girls”, null, msg.getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally{
channel.close();
connection.close();
}
}
public static void main(String[] args) {
try {
send(“hello rabbitmq”);
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
}
Comsumer:消费者
最后
看完美团、字节、腾讯这三家的面试问题,是不是感觉问的特别多,可能咱们又得开启面试造火箭、工作拧螺丝的模式去准备下一次的面试了。
开篇有提及我可是足足背下了1000道题目,多少还是有点用的呢,我看了下,上面这些问题大部分都能从我背的题里找到的,所以今天给大家分享一下互联网工程师必备的面试1000题。
注意不论是我说的互联网面试1000题,还是后面提及的算法与数据结构、设计模式以及更多的Java学习笔记等,皆可分享给各位朋友
互联网工程师必备的面试1000题
而且从上面三家来看,算法与数据结构是必备不可少的呀,因此我建议大家可以去刷刷这本左程云大佬著作的《程序员代码面试指南 IT名企算法与数据结构题目最优解》,里面近200道真实出现过的经典代码面试题。
}
Comsumer:消费者
最后
看完美团、字节、腾讯这三家的面试问题,是不是感觉问的特别多,可能咱们又得开启面试造火箭、工作拧螺丝的模式去准备下一次的面试了。
开篇有提及我可是足足背下了1000道题目,多少还是有点用的呢,我看了下,上面这些问题大部分都能从我背的题里找到的,所以今天给大家分享一下互联网工程师必备的面试1000题。
注意不论是我说的互联网面试1000题,还是后面提及的算法与数据结构、设计模式以及更多的Java学习笔记等,皆可分享给各位朋友
[外链图片转存中…(img-adENHHag-1721151951532)]
互联网工程师必备的面试1000题
而且从上面三家来看,算法与数据结构是必备不可少的呀,因此我建议大家可以去刷刷这本左程云大佬著作的《程序员代码面试指南 IT名企算法与数据结构题目最优解》,里面近200道真实出现过的经典代码面试题。
[外链图片转存中…(img-YVxiIJ0U-1721151951532)]