本文整理汇总了Java中com.rabbitmq.client.ConnectionFactory.newConnection方法的典型用法代码示例。如果您正苦于以下问题:Java ConnectionFactory.newConnection方法的具体用法?Java ConnectionFactory.newConnection怎么用?Java ConnectionFactory.newConnection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.rabbitmq.client.ConnectionFactory
的用法示例。
在下文中一共展示了ConnectionFactory.newConnection方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import com.rabbitmq.client.ConnectionFactory; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
ConnectionFactory factory = new ConnectionFactory();
factory.setUsername("guest");
factory.setPassword("guest");
factory.setVirtualHost("/");
factory.setHost("localhost");
factory.setPort(5672);
Connection newConnection = factory.newConnection();
Channel channel = newConnection.createChannel();
Scanner scanner = new Scanner(System.in);
String message = "";
while(!message.equals("exit")){
System.out.println("Enter your message");
message = scanner.next();
channel.queueDeclare("flink-test", true, false, false, null);
channel.basicPublish("", "flink-test", new BasicProperties.Builder()
.correlationId(java.util.UUID.randomUUID().toString()).build(), message.getBytes());
}
scanner.close();
channel.close();
newConnection.close();
}
开发者ID:PacktPublishing,项目名称:Practical-Real-time-Processing-and-Analytics,代码行数:26,代码来源:RMQPublisher.java
示例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: main
import com.rabbitmq.client.ConnectionFactory; //导入方法依赖的package包/类
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();
}
示例4: main
import com.rabbitmq.client.ConnectionFactory; //导入方法依赖的package包/类
public static void main(String[] argv) throws Exception {
ConnectionFactory factory = new ConnectionFactory();
// factory.setHost("");
factory.setUri("amqp://alpha.netkiller.cn");
factory.setUsername("admin");
// factory.setPassword("admin123");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "Hello World!";
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println(" [x] Sent '" + message + "'");
channel.close();
connection.close();
}
示例5: main
import com.rabbitmq.client.ConnectionFactory; //导入方法依赖的package包/类
public static void main(String[] args) throws NoSuchAlgorithmException, KeyManagementException, URISyntaxException, IOException, InterruptedException {
ConnectionFactory factory = new ConnectionFactory();
factory.setUri("amqp://guest:[email protected]");
factory.setConnectionTimeout(300000);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare("my-queue", true, false, false, null);
int count = 0;
while (count < 5000) {
String message = "Message number " + count;
channel.basicPublish("", "my-queue", null, message.getBytes());
count++;
System.out.println("Published message: " + message);
Thread.sleep(5000);
}
}
示例6: 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();
}
示例7: main
import com.rabbitmq.client.ConnectionFactory; //导入方法依赖的package包/类
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.DIRECT);
String severity = getSeverity(argv);
String message = getMessage(argv);
channel.basicPublish(EXCHANGE_NAME, severity, null, message.getBytes("UTF-8"));
System.out.println(" [x] Sent '" + severity + "':'" + message + "'");
channel.close();
connection.close();
}
示例8: updateTaskStatus
import com.rabbitmq.client.ConnectionFactory; //导入方法依赖的package包/类
private void updateTaskStatus(TaskStatus status) {
logger.info("[Study = " + taskStudy + "] [Unit = "+ unitId + "] Sending task update to server. Task id = [" + task.getId() + "] status = ["+status.toString()+"]");
final String QUEUE_NAME = SystemConstants.UBONGO_SERVER_TASKS_STATUS_QUEUE;
try {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(serverAddress);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
task.setStatus(status);
RabbitData message = new RabbitData(task, MachineConstants.UPDATE_TASK_REQUEST);
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
if (logger.isDebugEnabled()) {
logger.debug(" [!] Sent '" + message.getMessage() + "'");
}
channel.close();
connection.close();
} catch (Exception e){
logger.error("[Study = " + taskStudy + "] [Unit = "+ unitId + "] Failed sending task status to server. Task id = [" + task.getId() + "] Status = [" +
status.toString() + "] error: " + e.getMessage(), e);
}
}
示例9: main
import com.rabbitmq.client.ConnectionFactory; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException, TimeoutException {
//建立连接工厂
ConnectionFactory factory = new ConnectionFactory();
//设置连接地址
factory.setHost("seaof-153-125-234-173.jp-tokyo-10.arukascloud.io");
factory.setPort(31084);
//获取连接
Connection connection = factory.newConnection();
//获取渠道
Channel channel = connection.createChannel();
//声明队列,如果不存在就新建
//参数1队列名称;参数2是否持久化;参数3排他性队列,连接断开自动删除;参数4是否自动删除;参数5.参数
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
//发送的消息
String message = Thread.currentThread().getName() + "Hello ";
//参数1 交换机;参数2 路由键;参数3 基础属性;参数4 消息体
channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
System.out.println(Thread.currentThread().getName() + "[send]" + message);
channel.close();
connection.close();
}
示例10: 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();
}
}
示例11: main
import com.rabbitmq.client.ConnectionFactory; //导入方法依赖的package包/类
public static void main(String[] argv) {
Connection connection = null;
Channel channel = null;
try {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
connection = factory.newConnection();
channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
String routingKey = getRouting(argv);
String message = getMessage(argv);
channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes("UTF-8"));
System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'");
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (connection != null) {
try {
connection.close();
}
catch (Exception ignore) {}
}
}
}
示例12: main
import com.rabbitmq.client.ConnectionFactory; //导入方法依赖的package包/类
/**
* @param args
* @throws TimeoutException
* @throws IOException
* @throws InterruptedException
* @throws ConsumerCancelledException
* @throws ShutdownSignalException
*/
public static void main(String[] args) throws IOException, TimeoutException, ShutdownSignalException,
ConsumerCancelledException, InterruptedException {
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(RPC_QUEUE_NAME, false, false, false, null);
channel.basicQos(1);
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(RPC_QUEUE_NAME, false, consumer);
System.out.println("RPCServer Awating RPC request");
while (true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
BasicProperties props = delivery.getProperties();
BasicProperties replyProps = new AMQP.BasicProperties.Builder().correlationId(props.getCorrelationId())
.build();
String message = new String(delivery.getBody(), "UTF-8");
int n = Integer.parseInt(message);
System.out.println("RPCServer fib(" + message + ")");
String response = "" + fib(n);
channel.basicPublish("", props.getReplyTo(), replyProps, response.getBytes());
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
}
}
示例13: 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);
}
示例14: 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);
}
示例15: main
import com.rabbitmq.client.ConnectionFactory; //导入方法依赖的package包/类
/**
* @param args
* @throws TimeoutException
* @throws IOException
* @date 2017年7月11日 下午5:32:45
* @writer junehappylove
*/
public static void main(String[] args) throws IOException, TimeoutException {
// 创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
// 设置RabbitMQ地址
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(QUEUE_NAME, false, false, false, null);
System.out.println("Customer Waiting Received messages");
// DefaultConsumer类实现了Consumer接口,通过传入一个频道,
// 告诉服务器我们需要那个频道的消息,如果频道中有消息,就会执行回调函数handleDelivery
Consumer consumer = new DefaultConsumer(channel) {
//envelope主要存放生产者相关信息(比如交换机、路由key等)
//body是消息实体
@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("Customer Received '" + message + "'");
}
};
// 自动回复队列应答 -- RabbitMQ中的消息确认机制
channel.basicConsume(QUEUE_NAME, true, consumer);
}