public void Publish <T>(T item) { if (_connection == null || !_connection.IsOpen) { _connection = _factory.CreateConnection(); } using (IModel channel = _connection.CreateModel()) { channel.QueueDeclare(queue: queueName, durable: false, exclusive: false, autoDelete: false, arguments: null); var message = JsonConvert.SerializeObject(item); var body = Encoding.UTF8.GetBytes(message); channel.BasicPublish(exchange: "", routingKey: queueName, basicProperties: null, body: body); Published?.Invoke(this, new QueuePublisherEventArgs(message)); } }
internal static void OnPublished(DomainEventArgs args) { if (Published == null) { throw new NotImplementedException("No one has subscribed to this event."); } Published.Invoke(null, args); }
private void OnNotificationPublished(object sender, INotification notification) { // Add this facade to the notification to find it on restore var managed = (IManagedNotification)notification; managed.Source = Name; Published?.Invoke(this, notification); }
public void Publish(string news) { if (Published != null) { Published.Invoke(this, news); } // In C# 6.0 posso usare l'operatore Elvis sui metodi. // per fare i check sui null: // Published?.Invoke(this, news); }
/// <inheritdoc /> public void Publish(INotificationSender sender, INotification notification, object tag) { if (string.IsNullOrEmpty(sender.Identifier)) { throw new InvalidOperationException("The identifier of the sender must be set"); } if (notification == null) { throw new ArgumentNullException(nameof(notification), "Notification must be set"); } var managed = (IManagedNotification)notification; managed.Identifier = Guid.NewGuid(); managed.Created = DateTime.Now; managed.Sender = sender.Identifier; _listLock.EnterUpgradeableReadLock(); // Lets check if the notification was already published var isPending = _pendingPubs.Union(_pendingAcks).Union(_published) .Any(n => n.Notification.Identifier.Equals(notification.Identifier)); if (isPending) { _listLock.ExitUpgradeableReadLock(); throw new InvalidOperationException("Notification cannot be published twice!"); } _listLock.EnterWriteLock(); _pendingPubs.Add(new NotificationMap(sender, notification, tag)); _listLock.ExitWriteLock(); _listLock.ExitUpgradeableReadLock(); Published?.Invoke(this, notification); }
/// <inheritdoc /> void INotificationSourceAdapter.Sync() { // Publish pending notifications _listLock.EnterReadLock(); var pendingPublishs = _pendingPubs.ToArray(); _listLock.ExitReadLock(); foreach (var pendingPublish in pendingPublishs) { Published?.Invoke(this, pendingPublish.Notification); } // Acknowledge pending acknowledges _listLock.EnterReadLock(); var pendingAcks = _pendingAcks.ToArray(); _listLock.ExitReadLock(); foreach (var pendingAck in pendingAcks) { Acknowledged?.Invoke(this, pendingAck.Notification); } }
private void OnPublished() { Published?.Invoke(); }
public override void Publish(IMapsDirectlyToDatabaseTable databaseEntity) { base.Publish(databaseEntity); Published?.Invoke(databaseEntity); }
private void _Publisher_Published(string url, double timeSpent) { Published?.Invoke(url, timeSpent); }
public void DoPosting(string user, string password) { //login var koobecaClient = new KoobecaClient(); var koobecaService = new KoobecaService(koobecaClient); var getAccountTask = koobecaService.GetAccountAsync(user, password); Task.WaitAll(getAccountTask); var account = getAccountTask.Result; if (string.IsNullOrEmpty(account.oauth_token)) { Logger.Instance.Error("Failed to login to koobeca"); return; } //load the cache PageSources.GetByType("fb_page").Select(p => _PageSourceCache[p.source_id] = p).ToArray(); Logger.Instance.Debug($"page source cache loaded with {_PageSourceCache.Count} items."); //take from the queue while (IsActive) { if (_PostQueue.Count > 0) { var item = _PostQueue.Dequeue(); if (item.Type == FBPostType.Picture) { try { //make the picture include the bigger text StringBuilder bigMsg = new StringBuilder(); bigMsg.AppendLine(item.message); bigMsg.AppendLine("------------------------"); bigMsg.AppendLine(item.name); bigMsg.AppendLine(item.description); item.message = bigMsg.ToString(); //get all puctures List <string> picUrls = new List <string>(); picUrls.AddRange(item.attachments.data.Where(a => a.type == "photo").Select(a => a.media.image.src).ToList()); picUrls.AddRange(item.attachments.data.Where(a => a.type == "album").SelectMany(a => a.subattachments.data.Where(sa => sa.type == "photo").Select(sa => sa.media.image.src)).ToList()); var webClient = new WebClient(); item.Photos = new Stream[picUrls.Count]; for (int i = 0; i < picUrls.Count; i++) { var url = picUrls[i]; byte[] imageBytes = webClient.DownloadData(url); item.Photos[i] = new MemoryStream(imageBytes); } } catch (Exception e) { Logger.Instance.Error($"failed to get photo stream from {item.full_picture} , {e.Message}"); } } Logger.Instance.Info($"Got post from FB source:{item.source} of type {item.Type} , looking for target in Koobeca"); double timeSpent; PublishPost(_KoobecaService, item, out timeSpent); Published?.Invoke(item.source, timeSpent / 1000); // throw event to listeners } else { Thread.Sleep(1000); } } Logger.Instance.Info($"Publishing ended."); }