jeudi 2 mai 2019

Command pattern without Receiver

Is it fine to implement Command design pattern without a Receiver? In the ConcreteCommand execute method, I want to perform a static method call which does not need an object.

Does this web-app match MVC pattern or no?

I have a web-app (Java, Hibernate Spring [MVC, Security, ...], JSP) and the packets structure is this way:

  • entity (related to Hibernate)
  • dao (access to DB)
  • service (main logic)
  • serviceobjects (used in services)
  • controller
  • bean (sent from Controllers to user)
  • entitymapper & beanmapper (~converters)
  • reader & builder (special objects which do some business logic, used in services)
  • util (utils, like "get first element of Collection")

Does this structure match the MVC pattern?

  • If YES, then what is the M-Model here exactly?
  • If NO, then how could I describe such architecture?

Which design (using OOP) is better? Ensure reporting/logging of an object is externalized or internalized?

I am building this tool for comparison of properties in two resources that can be of type - JSON, properties, VMs, REST API, etc in Python.

The tool reads a metadata json having a structure like follows:

{
  "run-name": "Run Tests"
  "tests": [
    {
      "name": "Test 1",
      "checks":[
         {
          "name": "Dynamic Multiple",
          "type": "COMPARE",
          "dynamic": [
            {
              "file": "source.json",
              "type": "JSON",
              "property": "topology.wlsClusters.[].clusterName"
            }
          ],
          "source": {
            "file": "source.json",
            "type": "JSON",
            "property": "topology.wlsClusters.[clusterName == ${1}].Xms"
          },
          "target": {
            "file": "target.properties",
            "type": "PROPERTY",
            "property": "fusion.FADomain.${1}.default.minmaxmemory.main",
            "format": "-Xms{}?"
          }
        },
      ]
    }
  ]
}

mercredi 1 mai 2019

How to handle event based on their type

Say I have and event class that contains data and the type of the event (from remote service)

public class Event{
  String StringData;
  Integer IntData;
  EvenType eventType;
}

where EventType is enum :

public enum EventType{
 NEW,UPDATE,DELETE
}

Say I have a pipeline that recessives stream of event and need to process the data inside the event according to its type. For example if it is new event need to save to db and log if it is delete event save it to file , so each event type has its own behavior need to implement.

for example:

List<Event> events = ...
events.forEach(e->??? ); //how to process the event based on its type

Pattern for game design

I have exam in Java principles and patterns and i have this question for practicing. Could you please help me to understand patterns better. The question is :

Consider the design of a multi-player, interactive, real-time, video network game. In this game, each player can view his or her immediate surroundings in the game’s virtual world, see summary information on the opposing players in the game, and make moves in the game based on this knowledge. If an opposing player enters the field of view, this will be shown visually on the player’s screen. Players can join or depart at any time during the game.As players join, depart, and make moves in the game, the resulting game information must be updated for all current players.

Your job is to design the coordination portion of the game.

1)Select at least TWO design patterns you would apply to solve this problem? Why are they appropriate?

For the updating player i am thinking about Observer pattern since it can notify you when change occurs and i am not shure if it can be used to update player surroundings.Second pattern would be Component for example player actions.

2)Given your choice, provide a class diagram showing the structure and the relationships among the various classes.

Since i am not shure what patterns would i use i don't know what class or sequene diagram would i use

3)Provide a relevant subset of the methods in each class that will convince me that you understand how the pattern applies to this problem.

updatePlayerSurroundings() 
updateEnemyPlayerInformation()
moveForward()
jump()

ect...

4)Give a sequence diagram with two players and whatever other objects are necessary to show what happens when Player#1 makes a move and, as a result, becomes visible to Player#2.

If i am not mistaken this should be updated using observer?

How to process a kinesis stream record? (multiple proccessors)

I'm working on a project that monitors a micro-service based system. the mock micro-services I created produce data and upload it to Amazon Kinesis, now I use this code here from Amazon to produce to and consume from the Kinesis. But I have failed to understand how can I add more processors (workers) that will work on the same records list (possibly concurrently), meaning I'm trying to figure out where and how to plug in my code to the added code of Amazon I added here below. I'm going to have two processors in my program: 1) Will save each record to a DB. 2) Will update a GUI that will show monitoring of the system, given it can compare a current transaction to a valid transaction. My valid transactions will also be stored in a DB. meaning we will be able to see all of the data flow in the system and see how each request was handled from end to end.

I would really appreciate some guidance, as this is my first industry project and I'm also kind of new to AWS (though I have read about it a lot). Thanks!

Here is the code from amazon taken from this link: https://github.com/awslabs/amazon-kinesis-producer/blob/master/java/amazon-kinesis-producer-sample/src/com/amazonaws/services/kinesis/producer/sample/SampleConsumer.java

/*
 * Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Amazon Software License (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 * http://aws.amazon.com/asl/
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package com.amazonaws.services.kinesis.producer.sample;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.services.kinesis.clientlibrary.interfaces.IRecordProcessor;
import com.amazonaws.services.kinesis.clientlibrary.interfaces.IRecordProcessorCheckpointer;
import com.amazonaws.services.kinesis.clientlibrary.interfaces.IRecordProcessorFactory;
import com.amazonaws.services.kinesis.clientlibrary.lib.worker.InitialPositionInStream;
import com.amazonaws.services.kinesis.clientlibrary.lib.worker.KinesisClientLibConfiguration;
import com.amazonaws.services.kinesis.clientlibrary.lib.worker.Worker;
import com.amazonaws.services.kinesis.clientlibrary.lib.worker.ShutdownReason;
import com.amazonaws.services.kinesis.model.Record;

/**
 * If you haven't looked at {@link SampleProducer}, do so first.
 * 
 * <p>
 * As mentioned in SampleProducer, we will check that all records are received
 * correctly by the KCL by verifying that there are no gaps in the sequence
 * numbers.
 * 
 * <p>
 * As the consumer runs, it will periodically log a message indicating the
 * number of gaps it found in the sequence numbers. A gap is when the difference
 * between two consecutive elements in the sorted list of seen sequence numbers
 * is greater than 1.
 * 
 * <p>
 * Over time the number of gaps should converge to 0. You should also observe
 * that the range of sequence numbers seen is equal to the number of records put
 * by the SampleProducer.
 * 
 * <p>
 * If the stream contains data from multiple runs of SampleProducer, you should
 * observe the SampleConsumer detecting this and resetting state to only count
 * the latest run.
 * 
 * <p>
 * Note if you kill the SampleConsumer halfway and run it again, the number of
 * gaps may never converge to 0. This is because checkpoints may have been made
 * such that some records from the producer's latest run are not processed
 * again. If you observe this, simply run the producer to completion again
 * without terminating the consumer.
 * 
 * <p>
 * The consumer continues running until manually terminated, even if there are
 * no more records to consume.
 * 
 * @see SampleProducer
 * @author chaodeng
 *
 */
public class SampleConsumer implements IRecordProcessorFactory {
    private static final Logger log = LoggerFactory.getLogger(SampleConsumer.class);

    // All records from a run of the producer have the same timestamp in their
    // partition keys. Since this value increases for each run, we can use it
    // determine which run is the latest and disregard data from earlier runs.
    private final AtomicLong largestTimestamp = new AtomicLong(0);

    // List of record sequence numbers we have seen so far.
    private final List<Long> sequenceNumbers = new ArrayList<>();

    // A mutex for largestTimestamp and sequenceNumbers. largestTimestamp is
    // nevertheless an AtomicLong because we cannot capture non-final variables
    // in the child class.
    private final Object lock = new Object();

    /**
     * One instance of RecordProcessor is created for every shard in the stream.
     * All instances of RecordProcessor share state by capturing variables from
     * the enclosing SampleConsumer instance. This is a simple way to combine
     * the data from multiple shards.
     */
    private class RecordProcessor implements IRecordProcessor {
        @Override
        public void initialize(String shardId) {}

        @Override
        public void processRecords(List<Record> records, IRecordProcessorCheckpointer checkpointer) {
            long timestamp = 0;
            List<Long> seqNos = new ArrayList<>();

            for (Record r : records) {
                // Get the timestamp of this run from the partition key.
                timestamp = Math.max(timestamp, Long.parseLong(r.getPartitionKey()));

                // Extract the sequence number. It's encoded as a decimal
                // string and placed at the beginning of the record data,
                // followed by a space. The rest of the record data is padding
                // that we will simply discard.
                try {
                    byte[] b = new byte[r.getData().remaining()];
                    r.getData().get(b);
                    seqNos.add(Long.parseLong(new String(b, "UTF-8").split(" ")[0]));
                } catch (Exception e) {
                    log.error("Error parsing record", e);
                    System.exit(1);
                }
            }

            synchronized (lock) {
                if (largestTimestamp.get() < timestamp) {
                    log.info(String.format(
                            "Found new larger timestamp: %d (was %d), clearing state",
                            timestamp, largestTimestamp.get()));
                    largestTimestamp.set(timestamp);
                    sequenceNumbers.clear();
                }

                // Only add to the shared list if our data is from the latest run.
                if (largestTimestamp.get() == timestamp) {
                    sequenceNumbers.addAll(seqNos);
                    Collections.sort(sequenceNumbers);
                }
            }

            try {
                checkpointer.checkpoint();
            } catch (Exception e) {
                log.error("Error while trying to checkpoint during ProcessRecords", e);
            }
        }

        @Override
        public void shutdown(IRecordProcessorCheckpointer checkpointer, ShutdownReason reason) {
            log.info("Shutting down, reason: " + reason);
            try {
                checkpointer.checkpoint();
            } catch (Exception e) {
                log.error("Error while trying to checkpoint during Shutdown", e);
            }
        }
    }

    /**
     * Log a message indicating the current state.
     */
    public void logResults() {
        synchronized (lock) {
            if (largestTimestamp.get() == 0) {
                return;
            }

            if (sequenceNumbers.size() == 0) {
                log.info("No sequence numbers found for current run.");
                return;
            }

            // The producer assigns sequence numbers starting from 1, so we
            // start counting from one before that, i.e. 0.
            long last = 0;
            long gaps = 0;
            for (long sn : sequenceNumbers) {
                if (sn - last > 1) {
                    gaps++;
                }
                last = sn;
            }

            log.info(String.format(
                    "Found %d gaps in the sequence numbers. Lowest seen so far is %d, highest is %d",
                    gaps, sequenceNumbers.get(0), sequenceNumbers.get(sequenceNumbers.size() - 1)));
        }
    }

    @Override
    public IRecordProcessor createProcessor() {
        return this.new RecordProcessor();
    }

    public static void main(String[] args) {
        KinesisClientLibConfiguration config =
                new KinesisClientLibConfiguration(
                        "KinesisProducerLibSampleConsumer",
                        SampleProducer.STREAM_NAME,
                        new DefaultAWSCredentialsProviderChain(),
                        "KinesisProducerLibSampleConsumer")
                                .withRegionName(SampleProducer.REGION)
                                .withInitialPositionInStream(InitialPositionInStream.TRIM_HORIZON);

        final SampleConsumer consumer = new SampleConsumer();

        Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                consumer.logResults();
            }
        }, 10, 1, TimeUnit.SECONDS);

        new Worker.Builder()
            .recordProcessorFactory(consumer)
            .config(config)
            .build()
            .run();
    }
}

Is it a good practice to make an inner (nested) class in swift? [duplicate]

This question already has an answer here:

I'm not too experienced in swift, I am obj-c, but from time to time I need too mess with swift as well. I know in java for example it is useful for listeners, still may not be a best practice. So what about swift? My common sense tells me it is anti-pattern, in general. It could be useful for "quick development".