本文整理匯總了Java中com.rabbitmq.client.ConnectionFactory.setUsername方法的典型用法代碼示例。如果您正苦於以下問題:Java ConnectionFactory.setUsername方法的具體用法?Java ConnectionFactory.setUsername怎麽用?Java ConnectionFactory.setUsername使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.rabbitmq.client.ConnectionFactory
的用法示例。
在下文中一共展示了ConnectionFactory.setUsername方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: main
import com.rabbitmq.client.ConnectionFactory; //導入方法依賴的package包/類
/**
* @param args
* @throws IOException
* @throws TimeoutException
* @date 2017年7月11日 下午5:53:02
* @writer junehappylove
*/
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(host);
factory.setUsername(username);
factory.setPassword(password);
factory.setPort(port);
factory.setVirtualHost(virtualHost);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
// 分發信息
for (int i = 0; i < 20; i++) {
String message = "Hello RabbitMQ" + i;
channel.basicPublish("", TASK_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
System.out.println("NewTask send '" + message + "'");
}
channel.close();
connection.close();
}
示例2: main
import com.rabbitmq.client.ConnectionFactory; //導入方法依賴的package包/類
/**
* @param args
* @throws TimeoutException
* @throws IOException
* @date 2017年7月13日 下午2:49:49
* @writer junehappylove
*/
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(host);
factory.setUsername(username);
factory.setPassword(password);
factory.setPort(port);
factory.setVirtualHost(virtualHost);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
// 聲明交換機
channel.exchangeDeclare(EXCHANGE_NAME_ROUTING, "direct");// 注意是direct
// 發送信息
for (String routingKey : routingKeys) {
String message = "RoutingSendDirect Send the message level:" + routingKey;
channel.basicPublish(EXCHANGE_NAME_ROUTING, routingKey, null, message.getBytes());
System.out.println("RoutingSendDirect Send" + routingKey + "':'" + message);
}
channel.close();
connection.close();
}
示例3: create
import com.rabbitmq.client.ConnectionFactory; //導入方法依賴的package包/類
static ConnectionProvider create(final RabbitMqConfig config, final DefaultSslConfigurator sslConfigurator) {
final ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setUsername(config.username());
connectionFactory.setPassword(config.password());
connectionFactory.setVirtualHost(config.virtualHost());
connectionFactory.setAutomaticRecoveryEnabled(config.networkRecoveryEnabled());
connectionFactory.setTopologyRecoveryEnabled(config.topologyRecoveryEnabled());
connectionFactory.setConnectionTimeout((int) config.connectionTimeout().toMillis());
connectionFactory.setHandshakeTimeout((int) config.handshakeTimeout().toMillis());
connectionFactory.setShutdownTimeout((int) config.shutdownTimeout().toMillis());
connectionFactory.setNetworkRecoveryInterval(config.networkRecoveryInterval().toMillis());
connectionFactory.setRequestedHeartbeat((int) config.heartbeat().getSeconds());
connectionFactory.setRequestedChannelMax(config.channelLimit());
connectionFactory.setRequestedFrameMax(config.frameSizeLimit());
if (config.nonBlockingIoEnabled()) {
connectionFactory.useNio();
} else {
connectionFactory.useBlockingIo();
}
if (config.sslEnabled()) {
sslConfigurator.configure(connectionFactory, config);
}
return new RabbitConnectionProvider(config, connectionFactory);
}
示例4: send
import com.rabbitmq.client.ConnectionFactory; //導入方法依賴的package包/類
@POST
@Path("rabbitmqRecv")
public void send() throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setUsername("guest");
factory.setPassword("guest");
factory.setHost("127.0.0.1");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
DefaultConsumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
System.out.println(properties.getHeaders());
String message = new String(body, "UTF-8");
System.out.println(" [x] Received '" + message + "'");
}
};
channel.basicConsume(QUEUE_NAME, true, consumer);
}
示例5: main
import com.rabbitmq.client.ConnectionFactory; //導入方法依賴的package包/類
/**
* @param args
* @throws TimeoutException
* @throws IOException
* @date 2017年7月13日 下午3:08:40
* @writer junehappylove
*/
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(host);
factory.setUsername(username);
factory.setPassword(password);
factory.setPort(port);
factory.setVirtualHost(virtualHost);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
// 聲明一個匹配模式的交換器
channel.exchangeDeclare(EXCHANGE_NAME_TOPIC, "topic");
String queueName = channel.queueDeclare().getQueue();
// 路由關鍵字
String[] routingKeys = new String[]{"*.*.rabbit", "lazy.#"};
// 綁定路由關鍵字
for (String bindingKey : routingKeys) {
channel.queueBind(queueName, EXCHANGE_NAME_TOPIC, bindingKey);
System.out.println("ReceiveLogsTopic2 exchange:"+EXCHANGE_NAME_TOPIC+", queue:"+queueName+", BindRoutingKey:" + bindingKey);
}
System.out.println("ReceiveLogsTopic2 Waiting for messages");
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws UnsupportedEncodingException {
String message = new String(body, "UTF-8");
System.out.println("ReceiveLogsTopic2 Received '" + envelope.getRoutingKey() + "':'" + message + "'");
}
};
channel.basicConsume(queueName, true, consumer);
}
示例6: connectToBroker
import com.rabbitmq.client.ConnectionFactory; //導入方法依賴的package包/類
public void connectToBroker(){
try {
/*Get a ConnectionFactory object */
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(ip);
factory.setPort(port);
factory.setUsername(user);
factory.setPassword(password);
/* Create a connection */
connection = factory.newConnection();
/* Create a channel over that TCP/IP connection */
channel = connection.createChannel();
}
catch(Exception e){
e.printStackTrace();
}
}
示例7: createConnectionFactory
import com.rabbitmq.client.ConnectionFactory; //導入方法依賴的package包/類
private ConnectionFactory createConnectionFactory() throws IOException {
ConnectionFactory factory = new ConnectionFactory();
factory.setUsername(this.config.getUsername());
factory.setPassword(this.config.getPassword());
factory.setVirtualHost(this.config.getVirtualHost());
factory.setAutomaticRecoveryEnabled(true);
factory.setConnectionTimeout(this.config.getConnectionTimeout());
factory.setNetworkRecoveryInterval(this.config.getNetworkRecoveryInterval());
if (this.threadFactory != null) {
factory.setThreadFactory(this.threadFactory);
}
return factory;
}
示例8: main
import com.rabbitmq.client.ConnectionFactory; //導入方法依賴的package包/類
/**
* @param args
* @throws TimeoutException
* @throws IOException
* @date 2017年7月13日 下午3:03:24
* @writer junehappylove
*/
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = null;
Channel channel = null;
try {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(host);
factory.setUsername(username);
factory.setPassword(password);
factory.setPort(port);
factory.setVirtualHost(virtualHost);
connection = factory.newConnection();
channel = connection.createChannel();
// 聲明一個匹配模式的交換機
channel.exchangeDeclare(EXCHANGE_NAME_TOPIC, "topic");
// 待發送的消息
String[] routingKeys = new String[] { "quick.orange.rabbit", "lazy.orange.elephant", "quick.orange.fox",
"lazy.brown.fox", "quick.brown.fox", "quick.orange.male.rabbit", "lazy.orange.male.rabbit" };
// 發送消息
for (String severity : routingKeys) {
String message = "From " + severity + " routingKey' s message!";
channel.basicPublish(EXCHANGE_NAME_TOPIC, severity, null, message.getBytes());
System.out.println("TopicSend Sent '" + severity + "':'" + message + "'");
}
} catch (Exception e) {
e.printStackTrace();
if (connection != null) {
channel.close();
connection.close();
}
} finally {
if (connection != null) {
channel.close();
connection.close();
}
}
}
示例9: connection
import com.rabbitmq.client.ConnectionFactory; //導入方法依賴的package包/類
public static Connection connection() throws IOException, TimeoutException {
if(connection == null) {
synchronized(GabrielData.class) {
if(connection != null) return connection;
Config config = config();
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost(config.rabbitMQHost);
connectionFactory.setPort(config.rabbitMQPort);
connectionFactory.setUsername(config.rabbitMQUsername);
connectionFactory.setPassword(config.rabbitMQPassword);
connection = connectionFactory.newConnection();
generalPurposeChannel = connection.createChannel();
GatewayInfo.init(generalPurposeChannel);
}
}
return connection;
}
示例10: main
import com.rabbitmq.client.ConnectionFactory; //導入方法依賴的package包/類
/**
* @param args
* @throws IOException
* @throws TimeoutException
* @date 2017年7月13日 下午2:57:32
* @writer junehappylove
*/
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(host);
factory.setUsername(username);
factory.setPassword(password);
factory.setPort(port);
factory.setVirtualHost(virtualHost);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
// 聲明交換器
channel.exchangeDeclare(EXCHANGE_NAME_ROUTING, "direct");
// 獲取匿名隊列名稱
String queueName = channel.queueDeclare().getQueue();
// 根據路由關鍵字進行多重綁定
for (String severity : routingKeys2) {
channel.queueBind(queueName, EXCHANGE_NAME_ROUTING, severity);
System.out.println("ReceiveLogsDirect2 exchange:" + EXCHANGE_NAME_ROUTING + ", queue:" + queueName
+ ", BindRoutingKey:" + severity);
}
System.out.println("ReceiveLogsDirect2 Waiting for messages");
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws UnsupportedEncodingException {
String message = new String(body, "UTF-8");
System.out.println("ReceiveLogsDirect2 Received '" + envelope.getRoutingKey() + "':'" + message + "'");
}
};
channel.basicConsume(queueName, true, consumer);
}
示例11: main
import com.rabbitmq.client.ConnectionFactory; //導入方法依賴的package包/類
/**
* @param args
* @throws TimeoutException
* @throws IOException
* @date 2017年7月13日 下午2:53:18
* @writer junehappylove
*/
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(host);
factory.setUsername(username);
factory.setPassword(password);
factory.setPort(port);
factory.setVirtualHost(virtualHost);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
// 聲明交換器
channel.exchangeDeclare(EXCHANGE_NAME_ROUTING, "direct");
// 獲取匿名隊列名稱
String queueName = channel.queueDeclare().getQueue();
// 根據路由關鍵字進行綁定
for (String routingKey : routingKeys1) {
channel.queueBind(queueName, EXCHANGE_NAME_ROUTING, routingKey);
System.out.println("ReceiveLogsDirect1 exchange:" + EXCHANGE_NAME_ROUTING + "," + " queue:" + queueName
+ ", BindRoutingKey:" + routingKey);
}
System.out.println("ReceiveLogsDirect1 Waiting for messages");
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
String message = new String(body, "UTF-8");
System.out.println("ReceiveLogsDirect1 Received '" + envelope.getRoutingKey() + "':'" + message + "'");
}
};
channel.basicConsume(queueName, true, consumer);
}
示例12: main
import com.rabbitmq.client.ConnectionFactory; //導入方法依賴的package包/類
/**
* @param args
* @throws TimeoutException
* @throws IOException
* @date 2017年7月11日 下午5:21:46
* @writer junehappylove
*/
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(host);
factory.setUsername(username);
factory.setPassword(password);
factory.setPort(port);
factory.setVirtualHost(virtualHost);
Connection connection = null;
Channel channel = null;
try {
// 創建一個新的連接
connection = factory.newConnection();
// 創建一個通道
channel = connection.createChannel();
// 聲明一個隊列
// queueDeclare第一個參數表示隊列名稱
//第二個參數為是否持久化(true表示是,隊列將在服務器重啟時生存)
//第三個參數為是否是獨占隊列(創建者可以使用的私有隊列,斷開後自動刪除)
//第四個參數為當所有消費者客戶端連接斷開時是否自動刪除隊列
//第五個參數為隊列的其他參數
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "{\"temperature\":100}";
// 發送消息到隊列中
//basicPublish第一個參數為交換機名稱
//第二個參數為隊列映射的路由key
//第三個參數為消息的其他屬性
//第四個參數為發送信息的主體
channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8"));
System.out.println("Producer Send +'" + message + "'");
} catch (Exception e) {
e.printStackTrace();
} finally {
// 關閉通道和連接
channel.close();
connection.close();
}
}
示例13: main
import com.rabbitmq.client.ConnectionFactory; //導入方法依賴的package包/類
/**
* @param args
* @throws TimeoutException
* @throws IOException
* @date 2017年7月13日 下午2:40:52
* @writer junehappylove
*/
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(host);
factory.setUsername(username);
factory.setPassword(password);
factory.setPort(port);
factory.setVirtualHost(virtualHost);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
// 產生一個隨機的隊列名稱
String queueName = channel.queueDeclare().getQueue();
channel.queueBind(queueName, EXCHANGE_NAME, "");// 對隊列進行綁定
System.out.println("ReceiveLogs1 Waiting for messages");
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
String message = new String(body, "UTF-8");
System.out.println("ReceiveLogs1 Received '" + message + "'");
}
};
channel.basicConsume(queueName, true, consumer);// 隊列會自動刪除
}
示例14: setUp
import com.rabbitmq.client.ConnectionFactory; //導入方法依賴的package包/類
@Before
public void setUp() throws Exception {
connectionFactory = new ConnectionFactory();
// connectionFactory.setUri("amqp://guest:[email protected]:" +
// RABBITMQ_OUTSIDE_PORT + "/");
connectionFactory.setHost("localhost");
connectionFactory.setPort(6000);
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
connectionFactory.setVirtualHost("/");
Connection conn = null;
Channel channel = null;
try {
conn = connectionFactory.newConnection();
channel = conn.createChannel();
channel.queueDeclare(TEST_QUEUE, false, false, false, null);
channel.exchangeDeclare(TEST_QUEUE, "direct");
channel.queueBind(TEST_QUEUE, TEST_QUEUE, TEST_QUEUE);
} catch (Exception e) {
throw new ContextedRuntimeException(e).addContextValue("queueName", TEST_QUEUE)
.addContextValue("connectionFactory", ToStringBuilder.reflectionToString(connectionFactory));
}
}
開發者ID:BreakTheMonolith,項目名稱:btm-DropwizardHealthChecks,代碼行數:27,代碼來源:RabbitMQHealthCheckTestIntegration.java
示例15: createFromRabbitMqConfig
import com.rabbitmq.client.ConnectionFactory; //導入方法依賴的package包/類
default Connection createFromRabbitMqConfig(RabbitMqConfig config){
LOGGER.info("Trying to connect to RabbitMq '{}:{}'.", config.host(), config.port());
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(config.host());
factory.setPort(config.port());
if (StringUtils.isNotBlank(config.login())) {
factory.setUsername(config.login());
factory.setPassword(config.password());
LOGGER.debug("Using login : {} [{}]", config.login(), config.password());
}
factory.setVirtualHost(config.virtualHost());
LOGGER.debug("Use virtualhost {}", config.virtualHost());
try {
//ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);
return factory.newConnection();
} catch (IOException e) {
throw new RuntimeException("Unable to create a consumeConnection to Rabbit " + config.host() + ":" + config.port(), e);
}
}