import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; public class AppSensorClient { private static final String QUEUE_NAME = "appsensor_events"; public void publishEvent(String eventType, String attackerIp) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); String message = "Event Type: " + eventType + ", Attacker IP: " + attackerIp; channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8")); System.out.println("Event Published: " + message); channel.close(); connection.close(); } } import com.rabbitmq.client.*; public class AppSensorSubscriber { private static final String QUEUE_NAME = "appsensor_events"; public void subscribe() throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); Consumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String eventMessage = new String(body, "UTF-8"); System.out.println("Event Received: " + eventMessage); // ... } }; channel.basicConsume(QUEUE_NAME, true, consumer); } }


上一篇:
下一篇:
切换中文