List of usage examples for com.rabbitmq.client ConnectionFactory ConnectionFactory
ConnectionFactory
From source file:com.github.hexsmith.rabbitmq.producer.MessageProducer.java
License:Open Source License
public boolean sendMessage(String message) { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("127.0.0.7"); Connection connection = null; Channel channel = null;/* w w w .j a v a2s.c om*/ try { connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); logger.info("send message = {}", message); 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.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 www. ja va2 s. co 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 w w. jav a2 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;// www .j a v a 2 s . co m 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.googlecode.jmxtrans.model.output.RabbitMQWriter.java
License:Open Source License
private Channel createChannel() throws Exception { ConnectionFactory cf = new ConnectionFactory(); cf.setUri(this.uri); Connection connection = cf.newConnection(); return connection.createChannel(); }
From source file:com.googlesource.gerrit.plugins.rabbitmq.AMQPSession.java
License:Apache License
public void connect() { LOGGER.info("Connect to {}...", properties.getString(Keys.AMQP_URI)); ConnectionFactory factory = new ConnectionFactory(); try {/*from w ww .j a v a2s .com*/ if (StringUtils.isNotEmpty(properties.getString(Keys.AMQP_URI))) { factory.setUri(properties.getString(Keys.AMQP_URI)); if (StringUtils.isNotEmpty(properties.getString(Keys.AMQP_USERNAME))) { factory.setUsername(properties.getString(Keys.AMQP_USERNAME)); } if (StringUtils.isNotEmpty(properties.getString(Keys.AMQP_PASSWORD))) { factory.setPassword(properties.getString(Keys.AMQP_PASSWORD)); } connection = factory.newConnection(); connection.addShutdownListener(this); LOGGER.info("Connection established."); } //TODO: Consume review // setupConsumer(); } catch (URISyntaxException ex) { LOGGER.error("URI syntax error: {}", properties.getString(Keys.AMQP_URI)); } catch (IOException ex) { LOGGER.error("Connection cannot be opened."); } catch (Exception ex) { LOGGER.warn("Connection has something error. it will be disposed.", ex); } }
From source file:com.googlesource.gerrit.plugins.rabbitmq.session.type.AMQPSession.java
License:Apache License
@Override public void connect() { if (connection != null && connection.isOpen()) { LOGGER.info(MSG("Already connected.")); return;//from w w w .jav a 2s. com } AMQP amqp = properties.getSection(AMQP.class); LOGGER.info(MSG("Connect to {}..."), amqp.uri); ConnectionFactory factory = new ConnectionFactory(); try { if (StringUtils.isNotEmpty(amqp.uri)) { factory.setUri(amqp.uri); if (StringUtils.isNotEmpty(amqp.username)) { factory.setUsername(amqp.username); } Gerrit gerrit = properties.getSection(Gerrit.class); String securePassword = gerrit.getAMQPUserPassword(amqp.username); if (StringUtils.isNotEmpty(securePassword)) { factory.setPassword(securePassword); } else if (StringUtils.isNotEmpty(amqp.password)) { factory.setPassword(amqp.password); } connection = factory.newConnection(); connection.addShutdownListener(connectionListener); LOGGER.info(MSG("Connection established.")); } } catch (URISyntaxException ex) { LOGGER.error(MSG("URI syntax error: {}"), amqp.uri); } catch (IOException ex) { LOGGER.error(MSG("Connection cannot be opened."), ex); } catch (KeyManagementException | NoSuchAlgorithmException ex) { LOGGER.error(MSG("Security error when opening connection."), ex); } }
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 w w.jav a2s . c om } 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 w w .ja v a 2 s. com*/ connection.close(); }