Ejemplo n.º 1
0
        public override async Task UnregisterMessageAsync(Type type, Uri brokerServiceName = null, bool flushQueue = false)
        {
            if (brokerServiceName == null)
            {
                brokerServiceName = await PublisherServiceHelper.DiscoverBrokerServiceNameAsync();

                if (brokerServiceName == null)
                {
                    throw new InvalidOperationException("No brokerServiceName was provided or discovered in the current application.");
                }
            }
            var brokerService = await ServiceLocator.GetBrokerServiceForMessageAsync(type.Name, brokerServiceName);

            await brokerService.UnregisterSubscriberAsync(ActorReference.Get(ContextWrapper.Context), type.FullName, flushQueue);
        }
Ejemplo n.º 2
0
        public override async Task RegisterMessageAsync(Type type, Uri brokerServiceName = null, string listenerName = null)
        {
            if (brokerServiceName == null)
            {
                brokerServiceName = await PublisherServiceHelper.DiscoverBrokerServiceNameAsync();

                if (brokerServiceName == null)
                {
                    throw new InvalidOperationException("No brokerServiceName was provided or discovered in the current application.");
                }
            }

            var brokerService = await ServiceLocator.GetBrokerServiceForMessageAsync(type.Name, brokerServiceName);

            var serviceReference = CreateServiceReference(ContextWrapper.Context, partition, listenerName);
            await brokerService.RegisterServiceSubscriberAsync(serviceReference, type.FullName);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Publish the configured amount of messages.
        /// </summary>
        /// <param name="cancellationToken">Canceled when Service Fabric needs to shut down this service instance.</param>
        protected override async Task RunAsync(CancellationToken cancellationToken)
        {
            int    delay;
            string setting = GetConfigurationValue(Context, Messagesettings, "InitialDelaySec");

            if (!string.IsNullOrWhiteSpace(setting) && int.TryParse(setting, out delay))
            {
                ServiceEventSource.Current.ServiceMessage(this, $"Sleeping for {delay} seconds.");

                await Task.Delay(TimeSpan.FromSeconds(delay), cancellationToken);
            }


            int ammount;

            setting = GetConfigurationValue(Context, Messagesettings, "Amount");
            if (string.IsNullOrWhiteSpace(setting) || !int.TryParse(setting, out ammount))
            {
                return;
            }


            int messageTypeCount;

            setting = GetConfigurationValue(Context, Messagesettings, "MessageTypeCount");
            if (string.IsNullOrWhiteSpace(setting) || !int.TryParse(setting, out messageTypeCount))
            {
                return;
            }


            bool useConcurrentBroker = false;

            setting = GetConfigurationValue(Context, Messagesettings, "UseConcurrentBroker");
            if (!string.IsNullOrWhiteSpace(setting))
            {
                bool.TryParse(setting, out useConcurrentBroker);
            }

            AssemblyName asmName = new AssemblyName();

            asmName.Name = "Dynamic";
            AssemblyBuilder asmBuild = Thread.GetDomain().DefineDynamicAssembly(asmName, AssemblyBuilderAccess.RunAndSave);
            ModuleBuilder   modBuild = asmBuild.DefineDynamicModule("Module", "Dynamic.dll");
            List <Type>     types    = new List <Type>(messageTypeCount);

            for (int i = 0; i < messageTypeCount; i++)
            {
                string      messageTypeName = "DataContract" + i;
                TypeBuilder tb = modBuild.DefineType(messageTypeName, TypeAttributes.Public, typeof(Common.DataContracts.DataContract));
                tb.DefineDefaultConstructor(MethodAttributes.Public);
                types.Add(tb.CreateType());

                ServiceEventSource.Current.ServiceMessage(this, $"Created Message Type {messageTypeName} ({i} of {messageTypeCount}.");
            }

            Dictionary <Type, List <object> > messagesPerType = new Dictionary <Type, List <object> >();

            int basketCount = ammount / messageTypeCount;

            for (int i = 0; i < messageTypeCount; i++)
            {
                var messages = new List <object>();
                messagesPerType.Add(types[i], messages);

                for (int j = 0; j < basketCount; j++)
                {
                    var message = Activator.CreateInstance(types[i]);
                    messages.Add(message);
                }
            }

            var allMessages = messagesPerType.Values.SelectMany(l => l).ToArray();
            var options     = new ParallelOptions
            {
                MaxDegreeOfParallelism = -1
            };


            var helper  = new PublisherServiceHelper();
            var builder = new UriBuilder(Context.CodePackageActivationContext.ApplicationName);

            if (useConcurrentBroker)
            {
                builder.Path += "/ConcurrentBrokerService";
            }
            else
            {
                builder.Path += "/BrokerService";
            }
            var brokerSvcLocation = builder.Uri;

            ServiceEventSource.Current.ServiceMessage(this, $"Using Broker Service at '{brokerSvcLocation}'.");
            //sending starts here:

            Stopwatch sw = Stopwatch.StartNew();

            Parallel.For(0, allMessages.Length, options, i =>
            {
                var message = allMessages[i];
                helper.PublishMessageAsync(this, message, brokerSvcLocation).ConfigureAwait(false).GetAwaiter().GetResult();
            });
            sw.Stop();

            ServiceEventSource.Current.ServiceMessage(this, $"In {sw.ElapsedMilliseconds}ms - Published {ammount} instances of Message Types {string.Join(", ", types.Select(t => t.FullName))}.");

            while (!cancellationToken.IsCancellationRequested)
            {
                await Task.Delay(TimeSpan.FromSeconds(2), cancellationToken);
            }
        }