1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| public final static String QUEUE_NAME = "queue.scheduler";
public final static String EXCHANGE_NAME = "exchange.scheduler";
public final static String ROUTE_NAME = "route.scheduler";
public final static String DLX_QUEUE_NAME = "scheduler.queue.name";
public final static String DLX_EXCHANGE_NAME = "scheduler.exchange.name";
public static void main(String[] args) throws IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) { channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT, false, false, null); Map<String, Object> arguments = new HashMap<String, Object>(); arguments.put("x-dead-letter-exchange", DLX_EXCHANGE_NAME); arguments.put("x-message-ttl", 2000); channel.queueDeclare(QUEUE_NAME, false, false, false, arguments); channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTE_NAME); channel.exchangeDeclare(DLX_EXCHANGE_NAME, BuiltinExchangeType.DIRECT, false, false, null); channel.queueDeclare(DLX_QUEUE_NAME, false, false, false, null); channel.queueBind(DLX_QUEUE_NAME, DLX_EXCHANGE_NAME, ROUTE_NAME); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); channel.basicPublish(EXCHANGE_NAME, ROUTE_NAME, true, null, df.format(new Date()).getBytes()); } }
|