Refine search
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); } }
ConnectionFactory factory = new ConnectionFactory(); if (this.uri != null && !this.uri.isEmpty()){ try { factory.setUri(this.uri); } catch (URISyntaxException e) { LOG.error("Failed to parse uri", e); factory.setHost(this.host); factory.setPort(this.port); factory.setVirtualHost(this.virtualHost); factory.setUsername(this.username); factory.setPassword(this.password); factory.setAutomaticRecoveryEnabled(this.automaticRecovery); factory.setConnectionTimeout(this.connectionTimeout); factory.setNetworkRecoveryInterval(this.networkRecoveryInterval); factory.setRequestedHeartbeat(this.requestedHeartbeat); factory.setTopologyRecoveryEnabled(this.topologyRecovery); factory.setRequestedChannelMax(this.requestedChannelMax); factory.setRequestedFrameMax(this.requestedFrameMax);
this.host = host == null ? super.getHost() : host; this.port = port == 0 ? super.getPort() : port; this.username = username == null ? super.getUsername() : username; this.password = password == null ? super.getPassword() : password; this.virtualHost = virtualHost == null ? super.getVirtualHost() : virtualHost; this.uri = uri; this.requestedChannelMax = requestedChannelMax == 0 ? super.getRequestedChannelMax() : requestedChannelMax; this.requestedFrameMax = requestedFrameMax == 0 ? super.getRequestedFrameMax() : requestedFrameMax; this.requestedHeartbeat = requestedHeartbeat == 0 ? super.getRequestedHeartbeat() : requestedHeartbeat; this.connectionTimeout = connectionTimeout == 0 ? super.getConnectionTimeout() : connectionTimeout; this.clientProperties = clientProperties == null ? super.getClientProperties() : clientProperties; super.setHost(this.host); super.setPort(this.port); super.setUsername(this.username); super.setPassword(this.password); super.setVirtualHost(this.virtualHost); if (this.uri != null) { super.setUri(this.uri); super.setRequestedChannelMax(this.requestedChannelMax); super.setRequestedFrameMax(this.requestedFrameMax); super.setRequestedHeartbeat(this.requestedHeartbeat); super.setConnectionTimeout(this.connectionTimeout); super.setClientProperties(this.clientProperties);
public void connect() throws IOException { final ConnectionFactory factory = new ConnectionFactory(); factory.setHost(hostname); factory.setPort(port); factory.setVirtualHost(virtualHost); factory.setRequestedHeartbeat(heartbeatTimeout); factory.setAutomaticRecoveryEnabled(true); factory.useSslProtocol(); } catch (NoSuchAlgorithmException | KeyManagementException e) { throw new IOException("Couldn't enable TLS for AMQP input.", e); factory.setUsername(username); factory.setPassword(password); connection = factory.newConnection(); } catch (TimeoutException e) { throw new IOException("Timeout while opening new AMQP connection", e); channel = connection.createChannel(); channel.basicQos(prefetchCount); connection.addShutdownListener(cause -> { if (cause.isInitiatedByApplication()) { LOG.info("Shutting down AMPQ consumer.");
/** * 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); }
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(); } } }
@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; })); }
//executed once ConnectionFactory factory = new ConnectionFactory(); factory.setHost("someHost"); factory.setUsername("user"); factory.setPassword("pass"); Connection connection = factory.newConnection();
public static void main(String[] args) throws IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection();//工厂模式拿到connection Channel channel = connection.createChannel();//拿到channel channel.queueDeclare(QUEUE_NAME, false, false, false, null);//幂等地声明队列 String message = "HELLO WORLD!!!"; channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));//用channel发送消息(因为重要的api都在channel里面) System.out.println(" [x] Sent '" + message + "'"); channel.close();//关闭隧道 connection.close();//关闭和rabbitmq的server } }
tracker = new MessageTracker(); ConnectionFactory connFactory = new ConnectionFactory(); List<Address> addresses = CollectionUtils.transformToList(serverIps, new Function<Address, String>() { @Override connFactory.setAutomaticRecoveryEnabled(true); connFactory.setRequestedHeartbeat(CloudBusGlobalProperty.RABBITMQ_HEART_BEAT_TIMEOUT); connFactory.setNetworkRecoveryInterval((int) TimeUnit.SECONDS.toMillis(CloudBusGlobalProperty.RABBITMQ_NETWORK_RECOVER_INTERVAL)); connFactory.setConnectionTimeout((int) TimeUnit.SECONDS.toMillis(CloudBusGlobalProperty.RABBITMQ_CONNECTION_TIMEOUT)); connFactory.setUsername(CloudBusGlobalProperty.RABBITMQ_USERNAME); logger.info(String.format("use RabbitMQ username: %s", CloudBusGlobalProperty.RABBITMQ_USERNAME)); connFactory.setPassword(CloudBusGlobalProperty.RABBITMQ_PASSWORD); logger.info("use RabbitMQ password: ******"); connFactory.setVirtualHost(CloudBusGlobalProperty.RABBITMQ_VIRTUAL_HOST); logger.info(String.format("use RabbitMQ virtual host: %s", CloudBusGlobalProperty.RABBITMQ_VIRTUAL_HOST)); conn = connFactory.newConnection(addresses.toArray(new Address[]{})); logger.debug(String.format("rabbitmq connection is established on %s", conn.getAddress()));
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()); } }
private void createConnection() throws AiravataException { try { ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setUri(url); connectionFactory.setAutomaticRecoveryEnabled(true); connection = connectionFactory.newConnection(); connection.addShutdownListener(new ShutdownListener() { public void shutdownCompleted(ShutdownSignalException cause) { } }); log.info("connected to rabbitmq: " + connection + " for " + taskLaunchExchangeName); channel = connection.createChannel(); channel.basicQos(prefetchCount); // channel.exchangeDeclare(taskLaunchExchangeName, "fanout"); } catch (Exception e) { String msg = "could not open channel for exchange " + taskLaunchExchangeName; log.error(msg); throw new AiravataException(msg, e); } }
public void connect() throws IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); connection = factory.newConnection(); channel = connection.createChannel(); channel.exchangeDeclare(RabbitMqConsumer.EXCHANGE_NAME, "fanout", true); // publish/subscribe model System.out.println("Connected to RabbitMQ"); }
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()); cf.setPassword(context.getProperty(PASSWORD).getValue()); cf.setVirtualHost(vHost); cf.useSslProtocol(sslContext); cf.setSaslConfig(DefaultSaslConfig.EXTERNAL); Connection connection = cf.newConnection(); return connection; } catch (Exception e) {
public FirstMsgSender(String host, int port, String queueName) throws IOException, TimeoutException { super(); factory.setHost(host); factory.setPort(port); connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(queueName, true, false, false, null); this.queueName = queueName; }
public GetDeadLetterMessages(String queueName) throws Exception { this.queueName = queueName; factory = new ConnectionFactory(); factory.setHost(rabbitmq_host); connection = factory.newConnection(); channel = connection.createChannel(); //Add Dead letter queue channel.exchangeDeclare(queueName, "fanout", true); channel.queueDeclare(queueName, true, false, false, null); channel.queueBind(queueName, queueName, ""); consumer = new QueueingConsumer(channel); }
private Connection createConnection() throws IOException, TimeoutException, KeyManagementException, NoSuchAlgorithmException, URISyntaxException { final ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setUri(this.uri); connectionFactory.setRequestedHeartbeat(this.heartbeat); return connectionFactory.newConnection(); }
protected void setConnectionAndChannel(String connectionUri) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setUri(connectionUri); connection = factory.newConnection(); channel = connection.createChannel(); }
public void setup() throws IOException { logger.debug("setting up receiver.."); connFactory.setHost(host); connection = connFactory.newConnection(); channel = connection.createChannel(); channel.exchangeDeclare(exchange, "fanout"); queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, exchange, ""); tracingConsumer = new TracingConsumer(channel); cTag = channel.basicConsume(queueName, true, tracingConsumer); }
public static Channel init() throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("192.168.0.80"); factory.setUsername("root"); factory.setPassword("root"); factory.setPort(5672); connection = factory.newConnection(); channel = connection.createChannel(); return channel; }