this.clientProperties = clientProperties == null ? super.getClientProperties() : clientProperties; super.setHost(this.host); super.setPort(this.port); super.setUsername(this.username);
public void connect() throws IOException { final ConnectionFactory factory = new ConnectionFactory(); factory.setHost(hostname); factory.setPort(port); factory.setVirtualHost(virtualHost);
factory.setHost(this.host); factory.setPort(this.port); factory.setVirtualHost(this.virtualHost);
protected Connection createConnection(ProcessContext context) { final ConnectionFactory cf = new ConnectionFactory(); cf.setHost(context.getProperty(HOST).evaluateAttributeExpressions().getValue()); cf.setPort(Integer.parseInt(context.getProperty(PORT).evaluateAttributeExpressions().getValue())); cf.setUsername(context.getProperty(USER).evaluateAttributeExpressions().getValue());
public RabbitmqProducer(MaxwellContext context) { super(context); exchangeName = context.getConfig().rabbitmqExchange; props = context.getConfig().rabbitmqMessagePersistent ? MessageProperties.MINIMAL_PERSISTENT_BASIC : null; ConnectionFactory factory = new ConnectionFactory(); factory.setHost(context.getConfig().rabbitmqHost); factory.setPort(context.getConfig().rabbitmqPort); factory.setUsername(context.getConfig().rabbitmqUser); factory.setPassword(context.getConfig().rabbitmqPass); factory.setVirtualHost(context.getConfig().rabbitmqVirtualHost); try { this.channel = factory.newConnection().createChannel(); if(context.getConfig().rabbitmqDeclareExchange) { this.channel.exchangeDeclare(exchangeName, context.getConfig().rabbitmqExchangeType, context.getConfig().rabbitMqExchangeDurable, context.getConfig().rabbitMqExchangeAutoDelete, null); } } catch (IOException | TimeoutException e) { throw new RuntimeException(e); } }
@Test public void simpleRabbitMqTest() throws IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(rabbitMq.getContainerIpAddress()); factory.setPort(rabbitMq.getMappedPort(RABBITMQ_PORT)); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(RABBIQMQ_TEST_EXCHANGE, "direct", true); String queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, RABBIQMQ_TEST_EXCHANGE, RABBITMQ_TEST_ROUTING_KEY); // Set up a consumer on the queue final boolean[] messageWasReceived = new boolean[1]; channel.basicConsume(queueName, false, new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { messageWasReceived[0] = Arrays.equals(body, RABBITMQ_TEST_MESSAGE.getBytes()); } }); // post a message channel.basicPublish(RABBIQMQ_TEST_EXCHANGE, RABBITMQ_TEST_ROUTING_KEY, null, RABBITMQ_TEST_MESSAGE.getBytes()); // check the message was received assertTrue("The message was received", Unreliables.retryUntilSuccess(5, TimeUnit.SECONDS, () -> { if (!messageWasReceived[0]) { throw new IllegalStateException("Message not received yet"); } return true; })); }
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()); } }
public static void main(String[] args) { try { ConnectionFactory factory = new ConnectionFactory(); factory.setUsername("guest"); factory.setPassword("guest"); factory.setHost("60.205.191.82"); factory.setPort(5672); Connection conn = factory.newConnection(); Channel channel = conn.createChannel(); // channel.qu channel.queueDeclare("hello", false, false, false, null); String message = "Hello World!"; channel.basicPublish("", "hello", null, message.getBytes()); System.out.println(" [x] Sent '" + message + "'"); channel.close(); conn.close(); } catch (IOException e) { e.printStackTrace(); } catch (TimeoutException e) { e.printStackTrace(); } } }
/** * @param host the host. * @see com.rabbitmq.client.ConnectionFactory#setHost(java.lang.String) */ public void setHost(String host) { this.connectionFactory.setHost(host); }
/** * @param host the host. * @see com.rabbitmq.client.ConnectionFactory#setHost(java.lang.String) */ public void setHost(String host) { this.connectionFactory.setHost(host); }
@Bean public CachingConnectionFactory cf() throws Exception { ConnectionFactory cf = mock(ConnectionFactory.class); cf.setHost("localhost"); cf = spy(cf); willAnswer(i -> { this.connection = mock(Connection.class); willAnswer(ii -> { this.channel = mock(Channel.class); given(this.channel.isOpen()).willReturn(true); return this.channel; }).given(this.connection).createChannel(); return this.connection; }).given(cf).newConnection((ExecutorService) isNull(), anyString()); cf.setAutomaticRecoveryEnabled(false); CachingConnectionFactory ccf = new CachingConnectionFactory(cf); ccf.setSimplePublisherConfirms(true); return ccf; }
private void prepareRabbitMQConnection(String rabbitMQUrl) { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(rabbitMQUrl); try { connection = factory.newConnection(); } catch (IOException | TimeoutException e) { logger.error(e.getMessage(), e); throw new RuntimeException(e); } }
@Bean public ConnectionFactory connectionFactory() throws Exception { ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setHost(env.getProperty("mq.host").trim()); connectionFactory.setPort(Integer.parseInt(env.getProperty("mq.port").trim())); connectionFactory.setVirtualHost(env.getProperty("mq.vhost").trim()); connectionFactory.setUsername(env.getProperty("mq.username").trim()); connectionFactory.setPassword(env.getProperty("mq.password").trim()); return connectionFactory; }
/** * @param props RabbitMQ 配置信息 */ public RabbitMQClusterPolicy(Properties props){ this.exchange = props.getProperty("exchange", "j2cache"); factory = new ConnectionFactory(); factory.setHost(props.getProperty("host" , "127.0.0.1")); factory.setPort(Integer.valueOf(props.getProperty("port", "5672"))); factory.setUsername(props.getProperty("username" , null)); factory.setPassword(props.getProperty("password" , null)); //TODO 更多的 RabbitMQ 配置 }
public static Channel connect() throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("193.163.91.102"); factory.setPort(32768); Connection conn = factory.newConnection(); return conn.createChannel(); }
/** * Sets the ConnectionFactory parameters */ private void setFactoryConfiguration() { factory.setHost(this.host); factory.setPort(this.port); factory.setVirtualHost(this.virtualHost); factory.setUsername(this.username); factory.setPassword(this.password); }
@Bean QueueingConsumer queueingConsumer() throws IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(messageQueueHostname); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(Constants.ANSWERSHEET_DATA_QUEUE, true, false, false, null); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(Constants.ANSWERSHEET_DATA_QUEUE, true, consumer); return consumer; }
protected ConnectionFactory createFactory(Properties properties) { LOG.debug("Creating RabbitMQ connection factory with properties: {}", properties); ConnectionFactory factory = new ConnectionFactory(); factory.setUsername(USERNAME.readFrom(properties)); factory.setPassword(PASSWORD.readFrom(properties)); factory.setVirtualHost(VIRTUAL_HOST.readFrom(properties)); factory.setHost(HOST.readFrom(properties)); factory.setPort(Integer.parseInt(PORT.readFrom(properties))); factory.setExceptionHandler(handleChannelExceptions()); return factory; }
public void setup() throws IOException { connFactory.setHost("localhost"); connection = connFactory.newConnection(); channel = connection.createChannel(); channel.exchangeDeclare(exchange, "fanout"); }
public ConnectionFactory connectionFactory() { ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setHost(getHostIp()); connectionFactory.setPort(getPort()); connectionFactory.setUsername(getUsername()); connectionFactory.setPassword(getPassword()); return connectionFactory; }