List of usage examples for com.rabbitmq.client ConnectionFactory setHost
public void setHost(String host)
From source file:com.github.hexsmith.rabbitmq.producer.MessageProducer.java
License:Open Source License
public boolean sendMulitMessage(String message) { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("127.0.0.7"); Connection connection = null; Channel channel = null;//from w w w. j av a 2s. c o m try { connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); int i = 0; int loop = 0; String originalMessage = message; while (loop < 10000) { loop++; message += i++; channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); logger.info("send message = {}", message); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } message = originalMessage; } channel.close(); connection.close(); } catch (IOException | TimeoutException e) { logger.error("send message failed!,exception message is {}", e); return false; } return true; }
From source file:com.github.liyp.rabbitmq.demo2.Producer.java
License:Apache License
public static void main(String[] args) throws IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setUsername("liyp"); factory.setPassword("liyp"); factory.setVirtualHost("/"); factory.setHost("127.0.0.1"); factory.setPort(5672);/* w ww. j a v a 2 s .c om*/ Connection conn = factory.newConnection(); Channel channel = conn.createChannel(); for (int i = 0; i < 10000; i++) { byte[] messageBodyBytes = "Hello, world!".getBytes(); channel.basicPublish("", "my-queue", null, messageBodyBytes); System.out.println(channel.isOpen()); } channel.close(); conn.close(); }
From source file:com.github.liyp.rabbitmq.rpc.RPCClient.java
License:Apache License
public RPCClient() throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); connection = factory.newConnection(); channel = connection.createChannel(); replyQueueName = channel.queueDeclare().getQueue(); consumer = new QueueingConsumer(channel); channel.basicConsume(replyQueueName, true, consumer); }
From source file:com.github.liyp.rabbitmq.rpc.RPCServer.java
License:Apache License
public static void main(String[] argv) { Connection connection = null; Channel channel = null;/*ww w.j av a 2s.com*/ try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); connection = factory.newConnection(); 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(" [x] Awaiting RPC requests"); while (true) { String response = null; QueueingConsumer.Delivery delivery = consumer.nextDelivery(); BasicProperties props = delivery.getProperties(); BasicProperties replyProps = new BasicProperties.Builder().correlationId(props.getCorrelationId()) .build(); try { String message = new String(delivery.getBody(), "UTF-8"); int n = Integer.parseInt(message); System.out.println(" [.] fib(" + message + ")"); response = "" + fib(n); } catch (Exception e) { System.out.println(" [.] " + e.toString()); response = ""; } finally { channel.basicPublish("", props.getReplyTo(), replyProps, response.getBytes("UTF-8")); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } } } catch (Exception e) { e.printStackTrace(); } finally { if (connection != null) { try { connection.close(); } catch (Exception ignore) { } } } }
From source file:com.grnet.parsers.Entry.java
License:Open Source License
public static void main(String[] args) throws InterruptedException, InstantiationException, IllegalAccessException, ClassNotFoundException, LangDetectException { if (args.length != 3) { System.err.println("Usage : "); System.err.println("java -jar com.grnet.parsers.Entry <input folder> <output folder> <bad folder>"); System.exit(-1);/*from w ww. ja v a 2 s. c o m*/ } File input = new File(args[0]); File output = new File(args[1]); File bad = new File(args[2]); if (!input.exists() || !input.isDirectory()) { System.err.println("Input folder does not exist or it is not a folder."); System.exit(-1); } if (!output.exists() || !output.isDirectory()) { System.err.println("Output folder does not exist or it is not a folder."); System.exit(-1); } if (!bad.exists() || !bad.isDirectory()) { System.err.println("Bad files folder does not exist or it is not a folder."); System.exit(-1); } CheckConfig config = new CheckConfig(); if (config.checkAttributes()) { System.out.println("----------------------------------------"); System.out.println("Starting lang detection on folder:" + input.getName()); System.out.println("----------------------------------------"); String idClass = config.getProps().getProperty(Constants.inputClass); ClassLoader myClassLoader = ClassLoader.getSystemClassLoader(); Class myClass = myClassLoader.loadClass(idClass); Object whatInstance = myClass.newInstance(); Input inpt = (Input) whatInstance; Collection<File> data = (Collection<File>) inpt.getData(input); Stats stats = new Stats(data.size()); int threadPoolSize = Integer.parseInt(config.getProps().getProperty(Constants.tPoolSize)); int availableProcessors = Runtime.getRuntime().availableProcessors(); System.out.println("Available cores:" + availableProcessors); System.out.println("Thread Pool size:" + threadPoolSize); ExecutorService executor = Executors.newFixedThreadPool(threadPoolSize); long start = System.currentTimeMillis(); Iterator<File> iterator = data.iterator(); DetectorFactory.loadProfile(config.getProps().getProperty(Constants.profiles)); String strict = config.getProps().getProperty(Constants.strict); ConnectionFactory factory = new ConnectionFactory(); factory.setHost(config.getProps().getProperty(Constants.queueHost)); factory.setUsername(config.getProps().getProperty(Constants.queueUser)); factory.setPassword(config.getProps().getProperty(Constants.queuePass)); while (iterator.hasNext()) { Worker worker = new Worker(iterator.next(), config.getProps(), output.getPath(), bad.getPath(), stats, slf4jLogger, strict, QUEUE_NAME, factory); executor.execute(worker); } executor.shutdown(); executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); long end = System.currentTimeMillis(); long diff = end - start; System.out.println("Duration:" + diff + "ms"); System.out.println("Done"); if (config.getProps().getProperty(Constants.report).equalsIgnoreCase("true")) { try { System.out.println("Creating report..."); report report = new report(input.getName(), diff, threadPoolSize, availableProcessors, output, stats); report.createReport(); System.out.println("Done"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } else System.err.println("Please correct configuration.properties file attribute values"); }
From source file:com.groupx.recipientlist.test.RecipientList.java
public static void main(String[] argv) throws java.io.IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "fanout"); String message = getMessage(argv); channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes()); System.out.println(" [x] Sent '" + message + "'"); channel.close();//from w ww.j av a2 s . c o m connection.close(); }
From source file:com.groupx.recipientlist.test.TranslatorMock.java
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, "fanout"); String queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, EXCHANGE_NAME, ""); System.out.println(" [*] Waiting for messages. To exit press X+ENTER"); Consumer consumer = new DefaultConsumer(channel) { @Override//from w w w . ja v a2 s . c o m public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); System.out.println(" [x] Received '" + message + "'"); } }; channel.basicConsume(queueName, true, consumer); try { Scanner in = new Scanner(System.in); String input = in.next(); if ("x".equals(input)) { System.exit(0); } } catch (Exception e) { } }
From source file:com.hopped.runner.rabbitmq.RPCClient.java
License:Open Source License
/** * @param requestQueueName//from ww w. j av a2 s. com * @throws Exception */ public RPCClient(String requestQueueName) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); this.requestQueueName = requestQueueName; connection = factory.newConnection(); channel = connection.createChannel(); replyQueueName = channel.queueDeclare().getQueue(); consumer = new QueueingConsumer(channel); channel.basicConsume(replyQueueName, true, consumer); }
From source file:com.hopped.running.demo.Demo.java
License:Open Source License
public static void main(final String... strings) throws IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); AuthRequest request = AuthRequest.newBuilder().setUsername("hopped").build(); RunnerClient client = new RunnerClient(factory, "runningRabbit").init(); IRunnerService service = (IRunnerService) client.createProxy(IRunnerService.class); AuthResponse res = service.login(request); System.out.println("Returned sessionId: " + res.getSessionId()); Ack ack = service.setProfile(User.newBuilder().setAlias("hopped").build()); System.out.println(ack.getSuccess()); }
From source file:com.hopped.running.demo.RunServer.java
License:Open Source License
/** * //from ww w.jav a 2s . co m * @param args * @throws IOException */ public static void main(String[] args) throws IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); final String queueName = "runningRabbit"; Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.basicQos(1); channel.queueDeclare(queueName, false, false, false, null); ProtobufRPCServer server = new ProtobufRPCServer(channel, queueName).init() .setInstance(new RunnerServiceImpl()).setProtocol(IRunnerService.class); server.consume(); }