Search in sources :

Example 56 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project zipkin by openzipkin.

the class ITRabbitMQCollector method setup.

@BeforeEach
void setup() throws Exception {
    metrics.clear();
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(rabbit.host());
    factory.setPort(rabbit.port());
    connection = factory.newConnection();
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 57 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project yacy_grid_mcp by yacy.

the class RabbitQueueFactory method init.

private void init() throws IOException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setAutomaticRecoveryEnabled(true);
    factory.setHost(this.server);
    if (this.port > 0)
        factory.setPort(this.port);
    if (this.username != null && this.username.length() > 0)
        factory.setUsername(this.username);
    if (this.password != null && this.password.length() > 0)
        factory.setPassword(this.password);
    try {
        this.connection = factory.newConnection();
        // Map<String, Object> map = this.connection.getServerProperties();
        if (!this.connection.isOpen())
            throw new IOException("no connection");
        this.channel = connection.createChannel();
        if (!this.channel.isOpen())
            throw new IOException("no channel");
        this.queues = new ConcurrentHashMap<>();
    } catch (TimeoutException e) {
        throw new IOException(e.getMessage());
    }
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException)

Example 58 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project tech by ffyyhh995511.

the class MQServer method getConnection.

public static Connection getConnection() throws Exception {
    if (factory == null) {
        factory = new ConnectionFactory();
    }
    factory.setHost(org.tech.commons.util.PropertiesUtil.getProperties("rabbitmq.properties", "mqHost"));
    factory.setPort(Integer.parseInt(PropertiesUtil.getProperties("rabbitmq.properties", "mqPort")));
    factory.setUsername(PropertiesUtil.getProperties("rabbitmq.properties", "mqUsername"));
    factory.setPassword(PropertiesUtil.getProperties("rabbitmq.properties", "mqPassword"));
    // virtualHost 可以理解是命名空间
    factory.setVirtualHost(PropertiesUtil.getProperties("rabbitmq.properties", "virtualHost"));
    Connection connection = factory.newConnection();
    return connection;
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Connection(com.rabbitmq.client.Connection)

Example 59 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project tech by ffyyhh995511.

the class Send method main.

public static void main(String[] argv) throws java.io.IOException, TimeoutException, InterruptedException {
    System.out.println("----------main method is begin----------");
    // 创建工厂
    ConnectionFactory factory = new ConnectionFactory();
    System.out.println("----------factory successful!----------");
    // 设置IP,端口,账户和密码
    factory.setHost("127.0.0.1");
    factory.setPort(5672);
    // factory.setUsername("mq");
    // factory.setPassword("mq");
    // 创建连接对象
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    System.out.println("----------connected successful!----------");
    // 指定队列持久化
    channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
    String message = getMessage(argv);
    // 共发送10万条数据,每隔1S发送1次
    int index = 0;
    while (index < 100000) {
        Thread.sleep(1 * 100);
        channel.basicPublish("", TASK_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");
        index++;
    }
    channel.close();
    connection.close();
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection)

Example 60 with ConnectionFactory

use of com.rabbitmq.client.ConnectionFactory in project tech by ffyyhh995511.

the class EmitLog method main.

public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.FANOUT);
    String message = getMessage(argv);
    channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes("UTF-8"));
    System.out.println(" [x] Sent '" + message + "'");
    channel.close();
    connection.close();
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection)

Aggregations

ConnectionFactory (com.rabbitmq.client.ConnectionFactory)188 Connection (com.rabbitmq.client.Connection)87 Test (org.junit.Test)73 Channel (com.rabbitmq.client.Channel)63 IOException (java.io.IOException)53 TimeoutException (java.util.concurrent.TimeoutException)23 DefaultConsumer (com.rabbitmq.client.DefaultConsumer)11 Envelope (com.rabbitmq.client.Envelope)11 SSLContext (javax.net.ssl.SSLContext)11 QueueingConsumer (com.rabbitmq.client.QueueingConsumer)9 HashMap (java.util.HashMap)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 AMQP (com.rabbitmq.client.AMQP)6 Consumer (com.rabbitmq.client.Consumer)6 ShutdownSignalException (com.rabbitmq.client.ShutdownSignalException)6 AMQConnection (com.rabbitmq.client.impl.AMQConnection)6 Address (com.rabbitmq.client.Address)5 NioParams (com.rabbitmq.client.impl.nio.NioParams)4 AutorecoveringConnection (com.rabbitmq.client.impl.recovery.AutorecoveringConnection)4 Path (javax.ws.rs.Path)3