List of usage examples for com.rabbitmq.client Connection createChannel
Channel createChannel() throws IOException;
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);/*from ww w .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.RPCServer.java
License:Apache License
public static void main(String[] argv) { Connection connection = null; Channel channel = null;//from ww w. j av a2s .c o 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.grnet.parsers.Worker.java
License:Open Source License
@Override public void run() { // TODO Auto-generated method stub String name = xml.getName();// w ww . j av a 2 s .c om Document document; try { SAXBuilder builder = new SAXBuilder(); document = (Document) builder.build(xml); Element rootNode = document.getRootElement(); Record record = new Record(); record.setMetadata(rootNode); String elementsString = properties.getProperty(Constants.elements); String[] elements = elementsString.split(","); for (int i = 0; i < elements.length; i++) { List<Element> elementList = JDomUtils.getXpathList(elements[i], Namespace.getNamespace(properties.getProperty(Constants.prefix), properties.getProperty(Constants.uri)), record.getMetadata()); if (elementList != null) { for (int j = 0; j < elementList.size(); j++) { Element elmt = elementList.get(j); String titleText = elmt.getText(); if (!titleText.equals("")) { Attribute langAtt = elmt.getAttribute(properties.getProperty(Constants.attName)); String chosenLangAtt = properties.getProperty(Constants.attName); if (langAtt == null || langAtt.getValue().equals("") || langAtt.getValue().equals("none")) { StringBuffer logstring = new StringBuffer(); try { Detector detector = DetectorFactory.create(); detector.append(titleText); String lang = detector.detect(); Attribute attribute = new Attribute(chosenLangAtt, lang); elmt.setAttribute(attribute); stats.raiseElementsLangDetected(); logstring.append(xml.getParentFile().getName()); logstring.append(" " + name.substring(0, name.lastIndexOf("."))); logstring.append(" " + elements[i]); logstring.append(" " + lang); slf4jLogger.info(logstring.toString()); System.out.println("Opening queue connection..."); Connection connection = this.factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(this.queue, false, false, false, null); channel.basicPublish("", this.queue, null, logstring.toString().getBytes()); channel.close(); connection.close(); System.out.println("Opening queue connection..."); stats.addElementD(elements[i]); flag = true; } catch (LangDetectException e) { // TODO Auto-generated catch block // e.printStackTrace(); logstring.append(xml.getParentFile().getName()); logstring.append(" " + name.substring(0, name.lastIndexOf("."))); logstring.append(" " + "NoLangDetected"); slf4jLogger.info(logstring.toString()); Connection connection = this.factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(this.queue, false, false, false, null); channel.basicPublish("", this.queue, null, logstring.toString().getBytes()); channel.close(); connection.close(); if (strict.equals("true")) recon = false; else { recon = true; continue; } } } } } } } if (recon) { String xmlString = JDomUtils.parseXml2string(record.getMetadata().getDocument(), null); OaiUtils.writeStringToFileInEncodingUTF8(xmlString, outputPath + File.separator + name); } else { String xmlString = JDomUtils.parseXml2string(record.getMetadata().getDocument(), null); OaiUtils.writeStringToFileInEncodingUTF8(xmlString, bad + File.separator + name); } if (flag) stats.raiseFilesLangDetected(); if (recon == false) stats.raiseFilessLangNotDetected(); } catch (JDOMException | IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }
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();/* w w w . j a v a2 s . com*/ 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 ww . j a v a2s. com*/ 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.running.demo.RunServer.java
License:Open Source License
/** * // w w w . ja v a 2s. c o 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(); }
From source file:com.hp.ov.sdk.messaging.msmb.services.MsmbConnectionManager.java
License:Apache License
public void startMsmb(final RestParams params) { Connection conn = null; Channel channel = null;//from ww w.java 2 s.c o m // validate args if (null == params) { throw new SDKInvalidArgumentException(SDKErrorEnum.invalidArgument, null, null, null, SdkConstants.APPLIANCE, null); } // check if connection object already exists in the map // msmbConnection = map.get(params.getHostname()); if (!map.containsKey(params.getHostname())) { // get client cert final RabbitMqClientCert mqClientCert = messagingCertificate .getRabbitMqClientCertificateKeyPair(params); // get CA cert CaCert caCert = messagingCertificate.getCACertificate(params); // get SSLContext SSLContext sslContext = CertificateStoreManager.getSslContext(mqClientCert, caCert); ConnectionFactory connectionFactory = RabbitMqClientConnectionFactory.getConnectionFactory(sslContext, params); connectionFactory.setConnectionTimeout(1000); try { conn = connectionFactory.newConnection(); channel = conn.createChannel(); } catch (final IOException e) { throw new SDKResourceNotFoundException(SDKErrorEnum.resourceNotFound, null, null, null, SdkConstants.APPLIANCE, null); } // put into map map.putIfAbsent(params.getHostname(), new MsmbConnection(conn, channel, MsmbState.START)); } }
From source file:com.hp.ov.sdk.messaging.scmb.services.ScmbConnectionManager.java
License:Apache License
public void startScmb(final RestParams params) { Connection conn = null; Channel channel = null;/*from w w w . j a v a 2 s . c o m*/ // validate args if (null == params) { throw new SDKInvalidArgumentException(SDKErrorEnum.invalidArgument, null, null, null, SdkConstants.APPLIANCE, null); } final String sessionId = params.getSessionId(); // check if connection object already exists in the map // scmbConnection = map.get(params.getHostname()); if (!map.containsKey(params.getHostname())) { // get client cert final RabbitMqClientCert mqClientCert = messagingCertificate .getRabbitMqClientCertificateKeyPair(params); // get CA cert CaCert caCert = messagingCertificate.getCACertificate(params); // get SSLContext SSLContext sslContext = CertificateStoreManager.getSslContext(mqClientCert, caCert); ConnectionFactory connectionFactory = RabbitMqClientConnectionFactory.getConnectionFactory(sslContext, params); connectionFactory.setConnectionTimeout(1000); try { conn = connectionFactory.newConnection(); channel = conn.createChannel(); } catch (final IOException e) { LOGGER.error("ScmbConnectionManager : startScmb: IOException"); throw new SDKScmbConnectionNotFoundException(SDKErrorEnum.scmbConnectionNotFound, null, null, null, SdkConstants.SCMB_CONNECTION, null); } // put into map map.putIfAbsent(params.getHostname(), new ScmbConnection(conn, channel, ScmbState.START)); } }
From source file:com.hpe.caf.worker.testing.QueueServicesFactory.java
License:Apache License
public static QueueServices create(final RabbitWorkerQueueConfiguration configuration, final String resultsQueueName, final Codec codec) throws IOException, TimeoutException { Connection connection = createConnection(configuration, new NoOpCallback()); Channel pubChan = connection.createChannel(); Channel conChan = connection.createChannel(); RabbitUtil.declareWorkerQueue(pubChan, configuration.getInputQueue()); RabbitUtil.declareWorkerQueue(conChan, resultsQueueName); return new QueueServices(connection, pubChan, configuration.getInputQueue(), conChan, resultsQueueName, codec);/*from w ww. ja v a2s . c o m*/ }