Search in sources :

Example 36 with DeliverCallback

use of com.rabbitmq.client.DeliverCallback in project rabbitmq-tutorials by rabbitmq.

the class Recv method main.

public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    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");
    DeliverCallback deliverCallback = (consumerTag, delivery) -> {
        String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
        System.out.println(" [x] Received '" + message + "'");
    };
    channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
    });
}
Also used : DeliverCallback(com.rabbitmq.client.DeliverCallback) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Connection(com.rabbitmq.client.Connection) Channel(com.rabbitmq.client.Channel) StandardCharsets(java.nio.charset.StandardCharsets) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) DeliverCallback(com.rabbitmq.client.DeliverCallback)

Example 37 with DeliverCallback

use of com.rabbitmq.client.DeliverCallback in project jeesuite-libs by vakinge.

the class RabbitmqConsumerAdapter method start.

@Override
public void start() throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("");
    factory.setPort(101);
    factory.setUsername("");
    factory.setPassword("");
    Connection connect = factory.newConnection();
    Channel channel = connect.createChannel();
    // 声明exchange
    DeclareOk declareOk = channel.exchangeDeclare("my.fanout3", "fanout", true);
    channel.exchangeDeclare("test", "fanout");
    String queueName = channel.queueDeclare().getQueue();
    channel.queueBind(queueName, "test", "");
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    DeliverCallback deliverCallback = (consumerTag, delivery) -> {
        String message = new String(delivery.getBody(), "UTF-8");
        System.out.println(" [x] Received '" + message + "'");
    };
    channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
    });
}
Also used : DeclareOk(com.rabbitmq.client.AMQP.Exchange.DeclareOk) DeliverCallback(com.rabbitmq.client.DeliverCallback) MQConsumer(com.jeesuite.amqp.MQConsumer) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Connection(com.rabbitmq.client.Connection) Channel(com.rabbitmq.client.Channel) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) DeclareOk(com.rabbitmq.client.AMQP.Exchange.DeclareOk) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) DeliverCallback(com.rabbitmq.client.DeliverCallback)

Example 38 with DeliverCallback

use of com.rabbitmq.client.DeliverCallback in project egeria-connector-sas-viya by odpi.

the class RepositoryEventMapper method start.

/**
 * {@inheritDoc}
 */
@Override
public void start() throws ConnectorCheckedException {
    super.start();
    final String methodName = "start";
    auditLog.logMessage(methodName, AuditCode.EVENT_MAPPER_STARTING.getMessageDefinition());
    if (!(repositoryConnector instanceof RepositoryConnector)) {
        raiseConnectorCheckedException(ErrorCode.EVENT_MAPPER_IMPROPERLY_INITIALIZED, methodName, null, repositoryConnector.getServerName());
    }
    this.catalogOMRSRepositoryConnector = (RepositoryConnector) this.repositoryConnector;
    try {
        this.catalogOMRSMetadataCollection = (MetadataCollection) catalogOMRSRepositoryConnector.getMetadataCollection();
    } catch (RepositoryErrorException e) {
        raiseConnectorCheckedException(ErrorCode.REST_CLIENT_FAILURE, methodName, e, catalogOMRSRepositoryConnector.getServerName());
    }
    this.typeDefStore = catalogOMRSMetadataCollection.getTypeDefStore();
    catalogOMRSMetadataCollection.setEventMapper(this);
    this.metadataCollectionId = catalogOMRSRepositoryConnector.getMetadataCollectionId();
    this.originatorServerName = catalogOMRSRepositoryConnector.getServerName();
    this.originatorServerType = catalogOMRSRepositoryConnector.getServerType();
    DeliverCallback deliverCallback = (consumerTag, delivery) -> {
        String message = new String(delivery.getBody(), "UTF-8");
        log.debug("SAS Catalog Event received: '{}':'{}'", delivery.getEnvelope().getRoutingKey(), message);
        try {
            Event<CatalogEventPayload> event = this.mapper.readValue(message, Event.class);
            String eventPayload = event.getPayloadAsString();
            try {
                processEvent(eventPayload);
            } catch (Exception e) {
                log.error("Could not process event payload.", e);
                return;
            }
        } catch (IOException e) {
            log.warn("Could not parse event payload. Reason: {}", e.getLocalizedMessage());
            return;
        }
    };
    try {
        channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
        });
    } catch (Exception e) {
        log.error("RabbitMQ basic consumer failed to start", e);
    }
}
Also used : CatalogType(org.odpi.openmetadata.connector.sas.event.model.catalog.CatalogType) ErrorCode(org.odpi.openmetadata.connector.sas.auditlog.ErrorCode) OMRSRepositoryConnector(org.odpi.openmetadata.repositoryservices.connectors.stores.metadatacollectionstore.repositoryconnector.OMRSRepositoryConnector) DeliverCallback(com.rabbitmq.client.DeliverCallback) RepositoryConnector(org.odpi.openmetadata.connector.sas.repository.connector.RepositoryConnector) CatalogEventPayload(org.odpi.openmetadata.connector.sas.event.model.catalog.CatalogEventPayload) LoggerFactory(org.slf4j.LoggerFactory) SASCatalogGuid(org.odpi.openmetadata.connector.sas.repository.connector.model.SASCatalogGuid) Relationship(org.odpi.openmetadata.repositoryservices.connectors.stores.metadatacollectionstore.properties.instances.Relationship) Event(org.odpi.openmetadata.connector.sas.event.model.Event) AttributeTypeDef(org.odpi.openmetadata.repositoryservices.connectors.stores.metadatacollectionstore.properties.typedefs.AttributeTypeDef) Connection(com.rabbitmq.client.Connection) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) MapperFeature(com.fasterxml.jackson.databind.MapperFeature) SASCatalogObject(org.odpi.openmetadata.connector.sas.repository.connector.mapping.SASCatalogObject) EntityMappingSASCatalog2OMRS(org.odpi.openmetadata.connector.sas.repository.connector.mapping.EntityMappingSASCatalog2OMRS) Instance(org.odpi.openmetadata.connector.sas.event.model.catalog.instance.Instance) EntityDetail(org.odpi.openmetadata.repositoryservices.connectors.stores.metadatacollectionstore.properties.instances.EntityDetail) Map(java.util.Map) Definition(org.odpi.openmetadata.connector.sas.event.model.catalog.definition.Definition) TypeDef(org.odpi.openmetadata.repositoryservices.connectors.stores.metadatacollectionstore.properties.typedefs.TypeDef) RelationshipMapping(org.odpi.openmetadata.connector.sas.repository.connector.mapping.RelationshipMapping) AuditCode(org.odpi.openmetadata.connector.sas.auditlog.AuditCode) ConnectionProperties(org.odpi.openmetadata.frameworks.connectors.properties.ConnectionProperties) Logger(org.slf4j.Logger) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) OMRSRepositoryEventMapperBase(org.odpi.openmetadata.repositoryservices.connectors.stores.metadatacollectionstore.repositoryeventmapper.OMRSRepositoryEventMapperBase) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) MetadataCollection(org.odpi.openmetadata.connector.sas.repository.connector.MetadataCollection) IOException(java.io.IOException) ConnectorCheckedException(org.odpi.openmetadata.frameworks.connectors.ffdc.ConnectorCheckedException) TypeDefStore(org.odpi.openmetadata.connector.sas.repository.connector.stores.TypeDefStore) List(java.util.List) RepositoryErrorException(org.odpi.openmetadata.repositoryservices.ffdc.exception.RepositoryErrorException) Channel(com.rabbitmq.client.Channel) EndpointProperties(org.odpi.openmetadata.frameworks.connectors.properties.EndpointProperties) OpenMetadataTopicListener(org.odpi.openmetadata.repositoryservices.connectors.openmetadatatopic.OpenMetadataTopicListener) RepositoryErrorException(org.odpi.openmetadata.repositoryservices.ffdc.exception.RepositoryErrorException) OMRSRepositoryConnector(org.odpi.openmetadata.repositoryservices.connectors.stores.metadatacollectionstore.repositoryconnector.OMRSRepositoryConnector) RepositoryConnector(org.odpi.openmetadata.connector.sas.repository.connector.RepositoryConnector) Event(org.odpi.openmetadata.connector.sas.event.model.Event) DeliverCallback(com.rabbitmq.client.DeliverCallback) IOException(java.io.IOException) IOException(java.io.IOException) ConnectorCheckedException(org.odpi.openmetadata.frameworks.connectors.ffdc.ConnectorCheckedException) RepositoryErrorException(org.odpi.openmetadata.repositoryservices.ffdc.exception.RepositoryErrorException)

Example 39 with DeliverCallback

use of com.rabbitmq.client.DeliverCallback in project amos2022ss02-audit-chain by amosproj.

the class AggregateConsumerClient method start.

public void start() throws IOException, TimeoutException {
    System.out.println("Starting to receive Messages.");
    Connection connection = this.factory.newConnection();
    Channel channel = connection.createChannel();
    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    DeliverCallback deliverCallback = (consumerTag, delivery) -> {
        AggregateMessage message;
        try {
            message = (AggregateMessage) deserializeMessage(delivery.getBody());
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        Vector<Message> messages = message.getMessages();
        for (int i = 0; i < message.getMessageSize(); i++) {
            Message single_message = messages.get(i);
            this.persistenceStrategy.StoreMessage(new JsonMessage(single_message.getSequence_number(), single_message.getMessage()));
            System.out.println(String.format("Received event %d with the content: %s", single_message.getSequence_number(), single_message.getMessage()));
        }
    };
    channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
    });
}
Also used : AggregateMessageFilePersistence(ProducerDummy.Persistence.AggregateMessageFilePersistence) DeliverCallback(com.rabbitmq.client.DeliverCallback) Message(ProducerDummy.Messages.Message) JsonMessage(ProducerDummy.Messages.JsonMessage) ObjectInputStream(java.io.ObjectInputStream) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) Connection(com.rabbitmq.client.Connection) Vector(java.util.Vector) ByteArrayInputStream(java.io.ByteArrayInputStream) Paths(java.nio.file.Paths) AggregateMessage(ProducerDummy.Messages.AggregateMessage) AbstractClient(ProducerDummy.Client.AbstractClient) Channel(com.rabbitmq.client.Channel) Message(ProducerDummy.Messages.Message) JsonMessage(ProducerDummy.Messages.JsonMessage) AggregateMessage(ProducerDummy.Messages.AggregateMessage) JsonMessage(ProducerDummy.Messages.JsonMessage) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) AggregateMessage(ProducerDummy.Messages.AggregateMessage) DeliverCallback(com.rabbitmq.client.DeliverCallback) Vector(java.util.Vector)

Example 40 with DeliverCallback

use of com.rabbitmq.client.DeliverCallback in project rabbitmq-tutorials by rabbitmq.

the class ReceiveLogs method main.

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 CTRL+C");
    DeliverCallback deliverCallback = (consumerTag, delivery) -> {
        String message = new String(delivery.getBody(), "UTF-8");
        System.out.println(" [x] Received '" + message + "'");
    };
    channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
    });
}
Also used : DeliverCallback(com.rabbitmq.client.DeliverCallback) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Connection(com.rabbitmq.client.Connection) Channel(com.rabbitmq.client.Channel) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) DeliverCallback(com.rabbitmq.client.DeliverCallback)

Aggregations

DeliverCallback (com.rabbitmq.client.DeliverCallback)59 Channel (com.rabbitmq.client.Channel)58 Connection (com.rabbitmq.client.Connection)53 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)50 StandardCharsets (java.nio.charset.StandardCharsets)21 IOException (java.io.IOException)18 TimeoutException (java.util.concurrent.TimeoutException)12 ConnectionFactoryUtils (com.hyf.testDemo.mq.ConnectionFactoryUtils)11 AMQP (com.rabbitmq.client.AMQP)9 Map (java.util.Map)6 TimeUnit (java.util.concurrent.TimeUnit)6 Date (java.util.Date)5 RabbitMQUtils (com.quan.framework.amqp.util.RabbitMQUtils)4 CancelCallback (com.rabbitmq.client.CancelCallback)4 AbstractClient (ProducerDummy.Client.AbstractClient)3 AggregateMessage (ProducerDummy.Messages.AggregateMessage)2 Message (ProducerDummy.Messages.Message)2 ThreadUtil (cn.hutool.core.thread.ThreadUtil)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 DeclareOk (com.rabbitmq.client.AMQP.Exchange.DeclareOk)2